- Home /
Cannot move rigidbody properly
Right now i can either have my object move forward and back or left and right but one works and one doesn't. I am also not sure how to use the clamp to limit up and down rotation. Any Tips?
void FixedUpdate () {
euler.x = Mathf.Clamp (euler.x, -90,90);
euler.x -= Input.GetAxis ("Mouse Y");
euler.y += Input.GetAxis ("Mouse X");
rigidbody.transform.rotation = Quaternion.Euler(euler * Sensitivity);
rigidbody.velocity = transform.forward * Input.GetAxis ("Vertical") * moveSpeed;
rigidbody.velocity = transform.right * Input.GetAxis ("Horizontal") * moveSpeed;
}
Have you tried printing the values of Input.GetAxis ("...")
? $$anonymous$$aybe that will provide some insight.
Are you actually checking if the user presses a button here?
You need to put the Debug.Log(rigidbody.velocity) after the last statement to see whats actually happening, it will spam the console but thats ok. You'll know why one works and other doesn't.
For reference you can see the FirstPersonController script Unity comes (import character controller package if you dont have it) with to see what you are doing differently, because he end result looks similar.
Answer by robertbu · Mar 21, 2014 at 07:49 PM
For a solution you need to add the two vectors together. All you have to do is add a '+' sign:
rigidbody.velocity = transform.forward * Input.GetAxis ("Vertical") * moveSpeed;
rigidbody.velocity += transform.right * Input.GetAxis ("Horizontal") * moveSpeed;
Note the '+=' on the second line.
There is a hidden GetComponent call in using 'rigidbody' so a slightly better solution would be:
Vector3 v = transform.forward * Input.GetAxis("Vertical") + transform.right * Input.GetAxis("Horizontal"));
rigidbody.velocity = v * moveSpeed;
Your answer
![](https://koobas.hobune.stream/wayback/20220613140821im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Rigidbody Disable Velocity/Movement? 1 Answer
velocity change? 0 Answers
Rigidbody velocity capped at 0.4 1 Answer
Ragdoll problems 1 Answer