- Home /
My rigidbody's y rotation is always 180
So I have been messing around in Unity for quite sometime and decided to make a 3rd person game. I finished making the camera rotate around the player(although it may be a bit buggy), and I just started on the character movement and rotation. The movement seems to be working fine, but the rotation is being kind of buggy. I made the rigidbody's Y rotation equal to the camera Y rotation so the camera is always seeing his back. I tried this, thinking it would work and the rigidbody is always staying at 180 degrees on the Y-axis. I have been trying to debug it for hours on end and I haven't figured out why this is happening the the rigidbody. The code for the movement script is below:
using UnityEngine; using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float playerSpeed = 7.0f;
public float gravity = 10;
private Vector3 moveDirection;
private Transform myTransform;
void Start() {
myTransform = transform;
}
// Update is called once per frame
void FixedUpdate () {
Debug.Log(Camera.main.transform.eulerAngles.y);
MovePlayer();
RotatePlayer();
}
void MovePlayer() {
moveDirection = new Vector3(playerSpeed * Input.GetAxis("Horizontal"), -gravity, playerSpeed * Input.GetAxis("Vertical"));
//Transform the vector3 to local space
moveDirection = transform.TransformDirection(moveDirection);
//set the velocity, so you can move
transform.rigidbody.velocity = new Vector3(moveDirection.x, transform.rigidbody.velocity.y, moveDirection.z);
rigidbody.AddForce (Vector3.up * -10);
}
void RotatePlayer() {
rigidbody.MoveRotation(new Quaternion(myTransform.rotation.x, Camera.main.transform.eulerAngles.y, myTransform.rotation.z, 0));
}
}
Answer by robertbu · Jul 30, 2013 at 11:16 PM
Your main problem is on line 33. You are constructing a new Quaternion. The values x, y, z, w are not degrees, but instead are values between 0 and 1 and are non-intuitive. Unless you understand Quaternions, it not advised directly interact with the components. Unity provides a variety of helper functions. You could change your code to:
rigidbody.MoveRotation(Quaternion.Euler(myTransform.rotation.x, Camera.main.transform.eulerAngles.y, myTransform.rotation.z));
But this may also cause you problems. There are multiple Euler angle representations for any physical representation. And the Euler angles you set are not necessarily the ones you get back out. For example do this:
transform.eulerAngles = Vector3(180,0,0);
Debug.Log(transform.eulerAngles);
The debug will output (0,180,180), the same physical rotation, but not the same angles. An you can see how the shift in representation could cause issues.
A common solution to your rotation problem is this:
Vector3 camDir = Camera.main.forward;
camDir.y = 0.0;
transform.rotation = Quaternion.LookRotation(camDir);
This fixed the rotation problem, and I must thank you very much for that, but now the player is rotated backwards and moving forward with no buttons pressed.
Which solution are you using? If you are using the first one with $$anonymous$$oveRotation(), a read of the reference says that it is a delta rotation rather than an absolute rotation. So the code above will not work. If you need the physics of the $$anonymous$$oveRotation(), something like this should work (untested):
Vector3 camDir = Camera.main.forward;
camDir.y = transform.y;
Quaternion q = Quaternion.FromToRotation(transform.forward, camDir);
rigidbody.$$anonymous$$oveRotation(q);
If you don't need the physics, just use the second solution.
I was trying to use the 3rd one but, i'll try the one you just told me now. Also, in the line:
camDir.y = transform.y;
isn't there supposed to be transform.rotation.y?
edit: Trying this one, the player will shake violently and the rotation will put him almost sideways.
I misread your comment above. What I just gave you is unlikely to solve your problem. How do you have your model setup? For most things in Unity, the expectation is that when the rotation of the model is (0,0,0), the forward of the character is facing positive 'z' and the feet of the character point to '-y'. If your character is not setup this way, then most example rotation code you get on this list will not work. You can get around the issue by changing the model in a modeling program or by using empty game object with (0,0,0) rotation as a parent. Scripts go on the empty game object.
Right now the player is only a capsule, I haven't started modelling yet, the rotation is set to (0, 0, 0). However I will try the empty parent thing.
Edit: Sigh Nope, nothing is working. I think I'll just have to use the given 3d Camera script with no readability