I’ve made a rigidbody controller for the game i’m developing, the movement is controlled by forces and not applied directly to velocity because i need a bit of acceleration/deceleration feel while moving and not snappier movements.
I’ve got a Vector3 that store the movement input called worldVec, and i used Vector3.ClampMagnitude to clamp the max velocity my character can move, since i’m using forces.
The problem began when i introduced jump in the controller, in fact Vector3.ClampMagnitude clamp also vertical movements making me unable to jump higher than what i would.
Is there a way to clamp only horizontal ( x and z ) movements?
Or perhaps there’s another way to limit the controller’s velocity without clamping the velocity?
Here’s the code semplified:
Thanks in advance.
Vector3 worldVec;
float jumpSpeed;
float moveSpeed;
float maxSpeed
bool canJump;
void Update()
{
// Vector input from a virtual joystick i have
worldVec = stick.GetVec3d()
//Jump button is pressed
if(jump)
{
if(IsGrounded())
canJump=true;
}
}
void FixedUpdate()
{
if (canJump)
{
player.velocity+= new Vector3(0,jumpSpeed,0);
canJump=false;
}
//Here i clamp the velocity
player.velocity=Vector3.ClampMagnitude(player.velocity, maxSpeed);
player.AddForce (worldVec * moveSpeed * Time.deltaTime);
}