I’ve been having a lot of trouble figuring out how to limit horizontal air velocity. For example, when I’m grounded I can do.
if(IsGrounded() && !_isJumping)
{
_velocity = _rigidBody.velocity;
_velocity += _groundVelocity;
// Limit velocity to my run speed
if(_velocity.magnitude > runSpeed)
{
_velocity.Normalize();
_velocity *= runSpeed;
}
_rigidBody.velocity = _velocity;
}
But when I’m jumping I have gravity to worry about. so I can’t limit my horizontal velocity without messing with my vertical velocity. I’m not great at math and really don’t know too much about vector math. My gravity is attracting towards a sphere like planet gravity so my up and forward vectors always change which makes it even harder for me to visualize what I need to do.
I’m using the standard jump velocity formula
Mathf.Sqrt(-gravity * targetJumpHeight * 2);
So this is what I have so far (this is in FixedUpdate() ):
// Apply velocity changes
if(IsGrounded() && !_isJumping)
{
// Not sure if this should be here
// but everything seems to work.
_velocity = _rigidBody.velocity;
_velocity += _groundVelocity;
// Limit velocity to run speed
if(_velocity.magnitude > runSpeed)
{
_velocity.Normalize();
_velocity *= runSpeed;
}
// I know people say don't change this directly, but I'm looking for
// pretty instant move speed and this does what I need. I am open to
// suggestions :)
_rigidBody.velocity = _velocity;
}
else // Air control
{
// If I add _velocity = _rigidbody.velocity here
// and then do _velocity += _airVelocity then when I try to limit
// my velocity to the run speed it slows down the gravity
// since I'm normalizing it and multiplying by the run speed. Gravity needs
// to be faster than the run speed over time.
_velocity = _airVelocity;
if(_velocity.magnitude > airSpeed)
{
_velocity.Normalize();
_velocity *= airSpeed;
}
// this just keeps adding velocity (obviously :) )
// which makes the player go extremely fast
// How do I make the horizontal movement velocity be limited
// by the run speed but allow gravity to keep it's previous
// values and continue to work as needed.
_velocity += _rigidBody.velocity;
_rigidBody.velocity = _velocity;
}
// Add Jump velocity after movement velocity
if(_isJumping && !_hasJumped)
{
// This gets me the right jump velocity but then I
// screw it up next frame when I try and limit
// the horizontal air velocity
_rigidBody.velocity = _velocity + _jumpVelocity;
_hasJumped = true;
}
// Call Attractor update
gravityAttractor.Attract(_transform, _rigidBody);
// Clear our velocities so we can recalculate next frame
_velocity = _groundVelocity = _airVelocity = Vector3.zero;
I’m more of a hobbyist programmer and don’t really have all the concepts down so any help is greatly appreciated
Thanks,
Artie