public void SetIsTryingToStickInDirection(Utility.Directions direction, bool resetFirst = false) { if (resetFirst) { isTryingToStick.Reset(); } switch (direction) { case Utility.Directions.Up: isTryingToStick.above = true; break; case Utility.Directions.Down: isTryingToStick.below = true; break; case Utility.Directions.Left: isTryingToStick.left = true; break; case Utility.Directions.Right: isTryingToStick.right = true; break; case Utility.Directions.Null: isTryingToStick.Reset(); break; } }
// Called by the component that this entity simulates the physics for // Should be called in the FixedUpdate() method of that MonoBehaviour // Therefore, should run every physics update, every ~0.02 seconds public void Update() { if (IsOutsideMap()) { Debug.LogError("PLAYER OUTSIDE MAP"); transformPosition = Vector2.zero; velocityX = 0; velocityY = 0; ResetInputVelocity(); return; } UpdateRayCastOrigins(transformPosition); velocityY = HandleGravity(velocityY); velocityY = HandleVerticalFriction(velocityY); velocityX = HandleHorizontalFriction(velocityX); Vector2 attemptedDisplacement = new Vector2(velocityX, velocityY) * Time.deltaTime; // Add velocity from character movement input attemptedDisplacement += new Vector2(inputVelocityX, inputVelocityY) * Time.deltaTime; collisionInfo.Reset(); // Check for and resolve collisions UpdateRayCastOrigins(transformPosition); if (attemptedDisplacement.y != 0) { HandleVerticalCollisions(ref attemptedDisplacement); } if (attemptedDisplacement.x != 0) { HandleHorizontalCollisions(ref attemptedDisplacement); } // Set new position transformPosition += attemptedDisplacement; // Reset inputVelocity, as this should be manually controlled by the character each frame ResetInputVelocity(); // Don't accumulate gravity if we're on the ground if (collisionInfo.below) { velocityY = 0; } }