- Home /
Dampening Movement while in the Air
Heya, just getting into game development here and I've got some incredibly rudimentary FPS movement written, and it works well! However, I want to implement a dampening effect to movement while in the air, or essentially limit air control, while not removing it entirely.
For example, if the player is running forward at full speed and jumps, I want them to be able to slow down to a stop mid air, but I don't want them to be able to move backward. If they're able to, the movement should be minimal.
My code is separated into two scripts, a controller, and a motor. Here's the movement controller:
` // Get player input for movement direction
float _xMov = Input.GetAxisRaw("Horizontal");
float _zMov = Input.GetAxisRaw("Vertical");
Vector3 moveHorizontal = transform.right * _xMov;
Vector3 moveVertical = transform.forward * _zMov;
// Final movement vector calculations
finalVelocity = (moveHorizontal + moveVertical).normalized * speed;
// Apply the final movement calculation
motor.Move(finalVelocity);`
And here's the code related to the motor for movement along the x-axis and z-axis:
public void Move(Vector3 _velocity)
{
velocity = _velocity;
}
public bool IsGrounded()
{
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
}
// Run every physics iteration
void FixedUpdate()
{
PreformMovement();
PreformRotation();
PreformJump();
}
void PreformMovement()
{
if(velocity != Vector3.zero)
{
rb.MovePosition(transform.position + velocity * Time.fixedDeltaTime);
}
}
If you'd rather see both scripts in their entirety, let me know! Thanks in advance.