- Home /
Rigidbody Deceleration Uneven
I'm making a script to add player acceleration and deceleration. After struggling to get it working with a character controller, I finally switched to a rigidbody and everything works perfectly, except for deceleration. I want to be able to set the acceleration, max speed, and deceleration as variables so I can tweak them to figure out what feels best. This is my move function. It's called every fixed update by the server, and takes in _inputDirection, which is a Vector2 of the client's movement inputs. The player also has a Vector3 called speed, which is their x and z speed.
Vector3 _direction = Vector3.zero; _direction += transform.right * _inputDirection.x; _direction += transform.forward * _inputDirection.y; // X-Axis if (_direction.x > 0) { if (speed.x < 0) { speed.x += _direction.x * (acceleration + deceleration); } else { speed.x += _direction.x * acceleration; } } else if (_direction.x < 0) { if (speed.x > 0) { speed.x += _direction.x * (acceleration + deceleration); } else { speed.x += _direction.x * acceleration; } } else { if (speed.x > 0) { speed.x = Mathf.Max(speed.x - deceleration, 0); } else { speed.x = Mathf.Min(speed.x + deceleration, 0); } } // Z-Axis if (_direction.z > 0) { if (speed.z < 0) { speed.z += _direction.z * (acceleration + deceleration); } else { speed.z += _direction.z * acceleration; } } else if (_direction.z < 0) { if (speed.z > 0) { speed.z += _direction.z * (acceleration + deceleration); } else { speed.z += _direction.z * acceleration; } } else { if (speed.z > 0) { speed.z = Mathf.Max(speed.z - deceleration, 0); } else { speed.z = Mathf.Min(speed.z + deceleration, 0); } } if (speed.magnitude > maxSpeed) { speed = speed.normalized * maxSpeed; } rb.MovePosition(transform.position + speed * Time.deltaTime);
The player accelerates towards their max speed, they decelerate towards 0 if they stop moving, and if they start moving in a direction opposite the one they're currently moving, they both accelerate towards that new direction and decelerate from their old one. The issue is that if I turn diagonally, pick up speed, and stop moving, the player will start decelerating, but towards the end of their deceleration, they'll start moving another direction. I assume this is because if they're moving diagonally, their speeds in the x and z direction are uneven, so as each direction decelerates, one reaches zero while the other still has some speed, and the player starts only moving along the axis that still has speed.
I've tried multiplying the speed by something like .9 instead of subtracting, which fixes this issue. The only issue with this is the player's speed will keep getting smaller and smaller, but it never reaches 0. I've tried setting it to 0 if it's under a certain value, but this leads to the same issue with the deceleration being uneven towards the end. Any help getting either subtraction or division of player speed to work would be greatly appreciated.
Your answer
Follow this Question
Related Questions
How to drag and drop a instantiate prefab on my mobile game 3D using touchs! 0 Answers
Ground clamping a rigidbody on slope 1 Answer
Make Rigidbody Character retain momentum 0 Answers
RigidBody.AddForce not working 1 Answer
Movement using rigidbody.velocity to apply a constant force until stop 1 Answer