Correctly move rigidbody at constant speed
This is currently my code. The object moves at the desired speed but using rb.velocity = movement * speed if it jump it remains glued to the ground. I can't understand how I can fix things.
private Vector3 movement;
private float speed = 3;
public bool isGrounded;
void Start()
{
rb = GetComponent();
}
void Update()
{
float Horizontal = Input.GetAxis("Horizontal");
float Vertical = Input.GetAxis("Vertical");
movement = transform.right * Horizontal + transform.forward * Vertical;
float origMagnitude = movement.magnitude;
movement.y = 0.0f;
movement = movement.normalized * origMagnitude;
if(isGrounded && Input.GetKeyDown(KeyCode.Space))
rb.AddForce((Vector3.up + movement) * JumpForce, ForceMode.Impulse);
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, rayDistance, layer))
isGrounded = true;
else
isGrounded = false;
}
private void FixedUpdate()
{
rb.velocity = movement * speed;
}