Hey so i got my code to make the player move and rotate with the camera, but now gravity isn’t being applied to the player. instead i accidently made it so you can fly, how might I fix this?
// Update is called once per frame
void Update()
{
GravityHandler();
OnMovement();
OnJump();
}
//Movement Listener
private void OnMovement()
{
currentMovementInput = inputManager.OnMovementInput();
currentMovement.x = currentMovementInput.x;
currentMovement.z = currentMovementInput.y;
currentMovement = cameraTransform.forward * currentMovement.z + cameraTransform.right * currentMovement.x;
currentMovement.y = 0f;
isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
characterController.Move(currentMovement * Time.deltaTime * movementSpeed);
}
//Gravity Handler
private void GravityHandler()
{
bool isFalling = currentMovement.y <= 0.0f || !isJumpPressed;
float fallMultiplier = 2.0f;
if (characterController.isGrounded)
{
float groundedGravity = -0.05f;
currentMovement.y = groundedGravity;
}
else if (isFalling)
{
float previousYVelocity = currentMovement.y;
float newYVelocity = currentMovement.y + (gravity * fallMultiplier * Time.deltaTime);
float nextYVelocity = Mathf.Max((previousYVelocity + newYVelocity) * 0.5f, -20.0f);
currentMovement.y = nextYVelocity;
}
else
{
float previousYVelocity = currentMovement.y;
float newYVelocity = currentMovement.y + (gravity * Time.deltaTime);
float nextYVelocity = (previousYVelocity + newYVelocity) * 0.5f;
currentMovement.y = nextYVelocity;
}
}