private void FixedUpdate() { Move(_moveDirection * Time.deltaTime * _stats.movementSpeed); bool wasGrounded = _grounded; _grounded = false; var contacts = new List <ContactPoint2D>(); _circleCollider.GetContacts(contacts); if (contacts.Count > 0) { _grounded = true; if (!wasGrounded) { _eventSystem.Dispatch(MovementEvent.GROUNDED); } } if (_wannaJump && _grounded) { _wannaJump = false; Debug.Log(_stats.jumpForce); _rigidbody.AddForce(new Vector2(0f, _stats.jumpForce)); } }
private Collider2D getTarget() { Collider2D[] colliders = new Collider2D[10]; int count = attackRangeCollider.GetContacts(colliders); if (count != 0) { Collider2D closest = colliders[0]; float closestDist = 10000; foreach (Collider2D collider in colliders) { if (collider == null) { continue; } float d = Vector3.Distance(transform.position, collider.transform.position); if (d < closestDist) { closest = collider; closestDist = d; } } return(closest); } else { return(null); } }
protected Collider2D getTarget() { Collider2D[] colliders = new Collider2D[10]; int count = attackRangeCollider.GetContacts(colliders); if (count != 0) { return(colliders[0]); } else { return(null); } }
void OnCollisionEnter2D(Collision2D collision) { Collider2D[] colliders = new Collider2D[1]; if (footCollider.GetContacts(colliders) > 0) { jumpTimer = -1f; grounded = true; } if (collision.gameObject.tag == "Enemy" || collision.gameObject.tag == "Laser" || collision.gameObject.tag == "CustomEnemy") { onDeath(); } }
public bool CanJump() { //return true; ContactPoint2D[] contacts = new ContactPoint2D[10]; CircleCollider2D collider = rb.GetComponent <CircleCollider2D>(); int count = collider.GetContacts(contacts); if (SceneManager.GetActiveScene().name.Equals("Maze") && !Settings.GetBool("Physics")) { return(true); } for (int i = 0; i < 10; i++) { float dx = collider.bounds.center.x - contacts[i].point.x; float dy = collider.bounds.center.y - contacts[i].point.y; if (dy > 0 && dy >= Mathf.Abs(dx) && i < count) { return(true); } } return(false); }
void Update() { timeToTestCapture -= Time.deltaTime; if (timeToTestCapture <= 0) { circleCollider2D.GetContacts(colliderContacts); Collider2D[] capturingShips = colliderContacts .Select(collider => collider) .Where(collider => collider != null && collider.name == "Health") .ToArray(); captureNumbers[0] = 0; captureNumbers[1] = 0; for (int i = 0; i < capturingShips.Length; i++) { Ship capturingShip = capturingShips[i].GetComponentInParent <Ship>(); captureNumbers[capturingShip.Team] += 1; } if (captureNumbers[0] > captureNumbers[1] && (currentTeam == 1 || !captured)) { captured = true; currentTeam = 0; Captured?.Invoke(currentTeam); } if (captureNumbers[1] > captureNumbers[0] && (currentTeam == 0 || !captured)) { captured = true; currentTeam = 1; Captured?.Invoke(currentTeam); } timeToTestCapture = captureInterval; } }
// Update is called once per frame void Update() { Collider2D[] colliders_inside = new Collider2D[10]; int cols_found = player_collider.OverlapCollider(walls_filter, colliders_inside); if (cols_found > 0) { Vector3 out_vec; ContactPoint2D[] points = new ContactPoint2D[10]; int pnumber = player_collider.GetContacts(points); if (pnumber > 0) { out_vec.x = gameObject.transform.position.x - points[0].point.x; out_vec.y = gameObject.transform.position.y - points[0].point.y;; out_vec.z = 0; out_vec.Normalize(); print(pnumber); movement_velocity = out_vec; transform.position += movement_velocity * Time.deltaTime; return; } } movement_velocity.x = 0; movement_velocity.y = 0; if (Input.GetKey(KeyCode.W)) { movement_velocity.y = speed; } if (Input.GetKeyUp(KeyCode.W)) { movement.y = 0f; movement_velocity.y = 0f; } if (Input.GetKey(KeyCode.S)) { movement_velocity.y = -speed; } if (Input.GetKeyUp(KeyCode.S)) { movement.y = 0f; movement_velocity.y = 0f; } if (Input.GetKey(KeyCode.D)) { movement_velocity.x = speed; } if (Input.GetKeyUp(KeyCode.D)) { movement.x = 0f; movement_velocity.x = 0f; } if (Input.GetKey(KeyCode.A)) { movement_velocity.x = -speed; } if (Input.GetKeyUp(KeyCode.A)) { movement.x = 0f; movement_velocity.x = 0f; } // cap velocity if (movement.magnitude > max_mov_velocity) { movement.Normalize(); movement *= max_mov_velocity; } movement += movement_velocity; rotation += angular_velocity; Mathf.Clamp(rotation, -max_rot_velocity, max_rot_velocity); // final rotate transform.rotation *= Quaternion.AngleAxis(rotation * Time.deltaTime, Vector3.up); // finally move transform.position += movement * Time.deltaTime; }