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?
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?
No, isKinimatic is set to false. Also, how do I move on the local axis, not the global axis?
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.
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));