My character have an rigidbody and move using addForce.
I want to restrict the speed hence I check the rigibody magnitude.
while when my character is running at the maxSpeed,
it fail to jump, or jump a bit and stop due to the magnitude.
how can I only restrict the speed on the x and z axis so that Imy character can jump correctly?
void moveCharacterAndSetAnimation(float horizontal, float vertical){
Vector3 movement = new Vector3 (horizontal, 0f, vertical);
setAnimation (animationId.running);
playerGameObject.transform.forward = Vector3.Normalize (movement);
rb.AddForce (movement*froceAmount,ForceMode.Acceleration);
if(rb.velocity.magnitude >maxSpeed){
rb.velocity = rb.velocity.normalized * maxSpeed;
}
}
private bool checkIsGrounded(){
return Physics.Raycast(playerGameObject.transform.position, -Vector3.up, distToGround + 0.1f);
}
public void checkJump(){
if((Input.GetKeyDown(KeyCode.Space)||Input.GetKeyDown(KeyCode.Mouse0))&&checkIsGrounded()){
jumpCharacter();
}
}
void jumpCharacter(){
rb.AddForce(playerGameObject.transform.up * 500, ForceMode.Impulse);
}