After added a rigidbody in a GameObject, it moves on rotation
I added a rigidbody to make my character stick on the ground.
However, right after I added it, the character(GameObject) moved as I rotated the screen by mouse movement.
For example, the character looks at the ground, it moves forward. If it looks at sky, it moves backward! If I do not rotate by mouse, but just move with WASD buttons, the character does not move to any direction, it just stop moving immediately when I stop pressing the key (one of WASD).
My Update() function are shown below. The rotation does not make movement of the character when I remove the rigidbody.
How can I solve this problem? Thanks for your help all the time.
void Update() {
// MOUSELOOK ANGLES (This makes unwanted character move)
yaw += speedH * Input.GetAxis("Mouse X");
pitch -= speedV * Input.GetAxis("Mouse Y");
// TO LOOK
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
return;
// Whatever below the line...
// KEY MOVEMENT
var x = Input.GetAxisRaw("Horizontal") * 0.1f;
var z = Input.GetAxisRaw("Vertical") * 0.1f;
// X FOR LEFT/RIGHT, Z FOR FWD/BWD
transform.position += new Vector3(x, 0, z);
//transform.Translate(x, 0, z); // This does not fix Y
...
Answer by rajavamsidhar_gvs · Mar 02, 2016 at 05:02 AM
Rigidbody>constraints>Freez positions>check X,Z. Rigidbody>constraints>Freez Rotations>check X,y,Z.
it works perfect.
Thanks, it does not move anymore! But I need to learn about the reason, thanks.
may be coz of two axis it is confused.try like this..pretty simple .,use character control for moving your object and use mouselook.cs for rotation of object.
Answer by Eno-Khaon · Mar 02, 2016 at 06:44 AM
The reason this occurs is simple, but easily overlooked.
Presumably, you're looking from a first-person perspective. If you're going based on a fairly simple approach, then your character is represented as a capsule, or at least has a capsule collider.
With that in mind, consider what happens when you turn left and right: Rotate a capsule on the Y-axis and there's no obvious change to it. It's still balanced on a rounded end.
However, if you look up and down and rotate the capsule to match, what would normally happen? With the capsule tilted, it tries to fall over. That's the result when physical interactions are applied to a tilted capsule, since it's no longer perfectly balanced. However, before it visibly leans, you're correcting its rotation again in your script.
Well, when you lean forward, you're effectively falling forward onto your face, but that's canceled out by your script preventing falling forward. However, the bit of momentum going towards falling over is applied as velocity to the character.
So what's the solution, then? Well, in Unity's example first-person controller script, the left/right and up/down axes are separated. Left and right rotate the character, while up and down rotate the camera instead. That way, you look up and down without actually physically leaning forward and backward.
$$anonymous$$y collider is exactly a capsule. Thanks for providing an advanced answer.