- Home /
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<Rigidbody>();
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;
}
You need to choose either AddForce
or velocity
and just stick to it. Using both doesn't mix well.
I'd like to add force, but I don't know how to move the object at the same speed with AddForce. Can you show me
What about just rb.AddForce( movement * forcePerSecond * Time.fixedDeltaTime );
?
Answer by tadadosi · May 20, 2020 at 04:48 PM
By doing movement.y = 0.0f and passing the entire movement Vector3 to your rb.velocity, you are basically denying your rigidbody physics on Y axis, this is what you should do:
using UnityEngine;
public class pappaController : MonoBehaviour
{
public float speed = 10f;
public float jumpForce = 5f;
public float rayDistance = 5f;
public LayerMask WhatIsGround;
private Rigidbody rb;
private Vector3 movement;
private bool isGrounded;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float Horizontal = Input.GetAxis("Horizontal");
float Vertical = Input.GetAxis("Vertical");
movement = (transform.right * Horizontal + transform.forward * Vertical) * speed; // multiply by speed and you got your movement ready
// removed this part, don't know what was the point of it
if (isGrounded && Input.GetKeyDown(KeyCode.Space))
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
RaycastHit hit;
isGrounded = Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, rayDistance, WhatIsGround);
}
private void FixedUpdate()
{
// y axis -> rb.velocity.y
rb.velocity = new Vector3(movement.x , rb.velocity.y, movement.z);
}
}
Thanks so much! (the only thing I think is, in this way along the X and Z axis the object will not be subject to external forces .. if for example it is hit by a car the object however will not move undergoing the impact) . That's why I wanted to use addForce but at this point I don't think it's possible to move the object at a fixed speed.
If you want to just use AddForce, you could leave the velocity untouched and clamp it to a max velocity by doing something like:
if (velocity.magnitude > maxVelocity)
velocity *= 0.99f; // $$anonymous$$ake this a variable and play with its value to get different clamping results.
Or you could set a condition to make your object react to the impact, it could be a bool and a timer with a fixed duration, if the object is hit you make the player lose control for a short time and allow the velocity to be handled by Unity.
I was unable to apply your clamp idea with a script, I tried this script, and it works but the speed fluctuates too much:
if(rb.velocity.sqr$$anonymous$$agnitude < speed)
rb.AddForce(movement * speed, Force$$anonymous$$ode.VelocityChange);
By bool you mean something like::
if (hit) rb.velocity = new Vector3 (rb.velocity.x, rb.velocity.y, rb.velocity.z);
could it work with a timer?
Your answer
Follow this Question
Related Questions
Rigidbody.velocity seems to be breaking jumping physics 0 Answers
Moving rigidbody (Player) with addForce or Velocity ? 1 Answer
VR Sword Physics, how to add force or velocity to the sword for impact when it hits an object 1 Answer
How to allow the player to change the mass/force of an object in the UI? Noob question 2 Answers