void HandleWallCollision() { Vector2 direction = new Vector2(mDeltaVelocity.x > 0f ? 1f : -1f, 0f); RaycastHit2D[] collisionResults = new RaycastHit2D[5]; int collisionCount = Physics2D.BoxCastNonAlloc(mPosition, mPlayerSize, 0f, direction, collisionResults, Mathf.Abs(mDeltaVelocity.x), CollisionLayer); bool collisionHappened = false; if (collisionCount > 0) { for (int index = 0; index < collisionCount; index++) { float yDistance = collisionResults[index].point.y - mPosition.y; float xSign = collisionResults[index].point.x < mPosition.x ? -1f : 1f; if ((Mathf.Abs(yDistance) < (mHalfPlayerSize.y - 0.0001f)) && (xSign == direction.x)) { mWallDirection = (int)xSign; collisionHappened = mWallCollision = true; mDeltaVelocity.x = mVelocity.x = 0f; mPosition.x = MathFloat.Round(collisionResults[index].point.x - (mHalfPlayerSize.x * direction.x), 2); break; } } } if (!collisionHappened) { mWallCollision = false; } }
void HandleGroundCollisions() { float ySpeed = mVelocity.y < 0f ? mDeltaVelocity.y : -0.02f; Vector2 direction = new Vector2(0f, mDeltaVelocity.y > 0f ? 1f : -1f); RaycastHit2D[] collisionResults = new RaycastHit2D[5]; int collisionCount = Physics2D.BoxCastNonAlloc(mPosition, mPlayerSize, 0f, direction, collisionResults, Mathf.Abs(ySpeed), CollisionLayer); bool collisionHappened = false; if (collisionCount > 0) { RaycastHit2D collision; mGrounded = true; for (int index = 0; index < collisionCount; index++) { collision = collisionResults[index]; float xDistance = collision.point.x - mPosition.x; if (Mathf.Abs(xDistance) < (mHalfPlayerSize.x - 0.0001f)) { collisionHappened = mGrounded = true; mDeltaVelocity.y = mVelocity.y = 0f; mPosition.y = MathFloat.Round(collision.collider.bounds.max.y + mHalfPlayerSize.y, 2); break; } } if ((false == collisionHappened) && (mVelocity.y < 0f)) { mGrounded = false; } } else { mGrounded = false; } }
public void round_w_s() => AdvancePC().SetFD_I(MathFloat.Round(FS));