- Home /
Rigidbody movement
Hi, I have a little problem (Not for me but absolutely for you) . My player is a ball/Sphere and it has 500 movement speed , now in a big plane platform when i move ball horizontally or vertically directions then if i press keyboard key for long time it will make it speed faster and more faster not limited . Can you please tell me how can i limit my speed for a long distance .
Here is how i am moving my player .
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
rigidbody.AddForce (movement * speed * Time.deltaTime);
Answer by jenci1990 · Dec 25, 2014 at 08:15 PM
Make maxSpeed variable: float maxSpeed = 10f;
and use this:
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
rigidbody.AddForce (movement * speed * Time.deltaTime);
Vector3 currentSpeed = rigidbody.velocity;
currentSpeed.x = Mathf.Clamp(currentSpeed.x, -1f*maxSpeed, maxSpeed);
currentSpeed.y = Mathf.Clamp(currentSpeed.y, -1f*maxSpeed, maxSpeed); //not important
currentSpeed.z = Mathf.Clamp(currentSpeed.z, -1f*maxSpeed, maxSpeed);
rigidbody.velocity = currentSpeed;
Your answer
Follow this Question
Related Questions
Issues Making Character Move With Rigidbody 1 Answer
Tweaking character movement 1 Answer
Rigid body child Player movement 0 Answers
A simple solution for constant rigidbody movement without changing and clamping velocity directly 2 Answers
Rigidbody player movement unrealistic velocity problem. 1 Answer