/// <summary> /// Called whenever our player hits anything. Handles collisions by adjusting velocity. /// We're working under the assumption that everything that we hit is square. /// </summary> private void ProcessCollision() { // DONE Vector2 sizeX = GetComponent <BoxCollider2D>().size; sizeX.x *= 0.7f; Vector2 sizeY = GetComponent <BoxCollider2D>().size; sizeY.y *= 0.7f; RaycastHit2D collision_x = Physics2D.BoxCast(_rb.position, sizeY, 0, new Vector2(1, 0), 1, _mask); RaycastHit2D collision_y = Physics2D.BoxCast(_rb.position, sizeX, 0, new Vector2(0, -1), 1, _mask); if (collision_y.collider != null) { if (_velocity.y < 0) { _velocity.y = 0; } CollisionDown.Invoke(collision_y.collider); } if (collision_x.collider != null) { _velocity.x = 0.1f; CollisionDown.Invoke(collision_x.collider); } }
/// <summary> /// Called whenever our player hits anything. Handles collisions by adjusting velocity. /// We're working under the assumption that everything that we hit is square. /// </summary> private void ProcessCollision() { // // Player BoxCollider2D playerBox = GameObject.Find("Player(Clone)").GetComponent <BoxCollider2D>(); Vector2 vectorDown = new Vector2(0, -1); Vector2 vectorRight = new Vector2(1, 0); // casting down Vector2 centerBoxCast = new Vector2(_rb.position.x + (playerBox.size.x / 2), _rb.position.y); Vector2 sizeBoxCast = new Vector2(playerBox.size.x / 2, playerBox.size.y / 2); RaycastHit2D downRayCast = Physics2D.BoxCast(centerBoxCast, sizeBoxCast, 0, vectorDown, playerBox.size.x / 2, _mask); // casting right RaycastHit2D rightRayCast = Physics2D.BoxCast(centerBoxCast, sizeBoxCast, 0, vectorRight, playerBox.size.x / 2, _mask); if (downRayCast.collider != null) { if (_velocity.y <= 0) { _velocity.y = 0; CollisionDown.Invoke(downRayCast.collider); } } if (rightRayCast.collider != null) { _velocity.x = 0; } }
/// <summary> /// Called whenever our player hits anything. Handles collisions by adjusting velocity. /// We're working under the assumption that everything that we hit is square. /// </summary> private void ProcessCollision() { RaycastHit2D RightCast = Physics2D.BoxCast(_rb.position, new Vector2(1, 0.5f), 0, new Vector2(1, 0), 0.1f, _mask, -Mathf.Infinity, Mathf.Infinity); RaycastHit2D DownCast = Physics2D.BoxCast(_rb.position, new Vector2(0.5f, 1), 0, new Vector2(0, -1), 0.1f, _mask, -Mathf.Infinity, Mathf.Infinity); if (RightCast) { _velocity.x = 0; } if (DownCast) { _velocity.y = Mathf.Max(0, _velocity.y); CollisionDown.Invoke(DownCast.collider); } }
/// <summary> /// Called whenever our player hits anything. Handles collisions by adjusting velocity. /// We're working under the assumption that everything that we hit is square. /// </summary> private void ProcessCollision() { // TODO fill me in Vector2 Box_size = new Vector2(GetComponent <BoxCollider2D>().size.x / 3, GetComponent <BoxCollider2D>().size.y); double distance_y = _velocity.y * Time.deltaTime * TimeScale; RaycastHit2D raycast_hit_y = Physics2D.BoxCast(_rb.position, Box_size, 0, new Vector2(0, -1), (float)distance_y, _mask, Mathf.Infinity); if (raycast_hit_y.collider != null) { _velocity.y = Mathf.Max(_velocity.y, 0f); CollisionDown.Invoke(raycast_hit_y.collider); } Box_size = new Vector2(GetComponent <BoxCollider2D>().size.x, GetComponent <BoxCollider2D>().size.y / 3); double distance_x = _velocity.x * Time.deltaTime * TimeScale; RaycastHit2D raycast_hit_x = Physics2D.BoxCast(_rb.position, Box_size, 0, new Vector2(1, 0), (float)distance_x, _mask, Mathf.Infinity); if (raycast_hit_x.collider != null) { _velocity.x = 0f; } }