- Home /
Rigidbody bumping when going down slopes
My rigidbody controller goes fine up a slope, but when I go down it, it bumps around. My collider is smooth around the edges. I use Rigidbody.MovePosition to move the player.
What is happening?
This usually happens when moving the rigidbody on a global axis ins$$anonymous$$d of it's local axis. Also is is$$anonymous$$inematic
set to true?
Yes, but it's over 500 lines long, so I'll just give you the movement stuff
Vector3 movement = transform.forward * $$anonymous$$ovementInputValue * -moveSpeed * Time.deltaTime;
Vector3 finished$$anonymous$$ovement = rb.position + movement;
if (which$$anonymous$$ovement == movementType.rb$$anonymous$$ovePosition)
rb.$$anonymous$$ovePosition(new Vector3(finished$$anonymous$$ovement.x, transform.position.y, finished$$anonymous$$ovement.z));
else if (which$$anonymous$$ovement == movementType.rbAddRelativeForce)
rb.$$anonymous$$ovePosition(new Vector3(finished$$anonymous$$ovement.x, transform.position.y, finished$$anonymous$$ovement.z));
Ins$$anonymous$$d of using RigidBody.$$anonymous$$ovePosition alone. You can use a lerp function to make it smoother.
`Rigidbody.$$anonymous$$ovePosition (Vector3.Lerp (rb.position, finished$$anonymous$$ovement, 1f));`
Answer by Cornelis-de-Jager · Aug 02, 2019 at 12:37 AM
Instead of using RigidBody.MovePosition alone. You can use a lerp function to make it smoother.
Rigidbody.MovePosition (Vector3.Lerp (rb.position, finishedMovement, 1f));
Alternatively you can move the object along the slope rather than the forward position.
HitInfo GetFloor() {
// Do raycast to get floor
Ray ray = new Ray (transform.position, vector3.down);
HitInfo hit;
Physics.Raycast (ray, out hit);
return hit;
}
Vector3 GetMoveDirection () {
var forward = transform.forward;
var floor = GetFloor();
if (floor.transform == null)
return forward;
var normal = floor.Normal;
var moveDir = new Vector3 (forward.x, -normal.y, for .z);
return moveDir.Normalize();
}
...
Vector3 movement = GetMoveDirection () * MovementInputValue * -moveSpeed * Time.deltaTime;
Vector3 finishedMovement = rb.position + movement;
if (whichMovement == movementType.rbMovePosition)
rb.MovePosition(new Vector3(finishedMovement.x, transform.position.y, finishedMovement.z));
else if (whichMovement == movementType.rbAddRelativeForce)
rb.MovePosition(new Vector3(finishedMovement.x, transform.position.y, finishedMovement.z));
No its C#, the bottom part should just replace some of the code you provided.
Answer by suIly · Jul 31, 2019 at 10:09 PM
No, isKinimatic is set to false. Also, how do I move on the local axis, not the global axis?
Answer by Magso · Jul 31, 2019 at 10:21 PM
In the docs for MovePosition it states that in order for a smooth transition it needs to be kinematic but that would be used for something like for example a 3rd person platformer enemy. A rigidbody ideally needs AddRelativeForce.
Thanks! The player movement is a lot smoother! What is the difference between the $$anonymous$$ovePosition and AddRelativeForce?
Basically $$anonymous$$ovePosition(Vector3.forward)
is teleporting the rigidbody forward then relying on the physics to correct it afterwards. AddRelativeForce
moves the rigidbody using the physics engine. $$anonymous$$ovePosition
is better for is$$anonymous$$inematic which is like a rigidbody ignored by the physics engine but has methods which allow it to affect and react with other rigidbodies.
The tank still bumps around when going downhill...?
Your answer
Follow this Question
Related Questions
Stick player (my own rigidbody controller) to the ground when going down the slope 2 Answers
MMD How to export model and animations to Unity as 3rd person controller? 2 Answers
turn on and off character controller 0 Answers
Platforming - moving up sloped surfaces ? 0 Answers
How can I change my movement scripts so it moves by rigidbodies movement so it doesn't clip 1 Answer