- Home /
Calculate which way you need to rotate in order to look to the left/right
I am trying to make a game where you can switch gravity to, let's say, be to the left instead of down. When doing that, I am also changing how the player jumps, and looks around. The gravity can change depending on other surfaces around the level, which may have the rotation in them set to anything.
How can you calculate which axis to rotate? For example, when rotation is (0,0,0), you need to rotate the Y axis to look to the left and right, with the pretty standard code:
yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
transform.eulerAngles = new Vector3(pitch, yaw);
In the editor, when you are in rotation mode, no matter what rotation an object has, you can have it rotate to the left and right. How can you programmatically do that?
Answer by Shrikky23 · Jul 04, 2017 at 02:43 PM
A general information : Yaw Pitch and Roll is not the same as what we think. Unity Quaternions convert Quats to Euler angles in order Z , X , Y.. Which means, your Pitch and Roll would not be the same when yaw changes. For example, Z / Rolll will never fail. But Pitch (Rotation around X axis) will always fail when you have different Non Zero Z and Y.
Also I am not 100% sure what you want, but if you want something to rotate On Left/Right
Get the Left or right vector by using this.transform.right (and -this.transform.right for Left vector)
Use Transform.Rotate, if you want to rotate the transform.
Use Quaternions to rotate generally, its much easier.
I want the player, no matter what the player's rotation is, to always rotate in the same way using the mouse. For example, if the player is upside down, left and right will be inverted.
after some testing, got that working doing transform.Rotate(new Vector3(1, 0, 0), angle);
. thanks!
Also when you transform.Rotate(Vector3.right * Time.deltaTime);
you can also add a parameter Space.World or Space.Self for global/local axis reference
Answer by efeguclu · Jul 04, 2017 at 01:55 PM
if I understand correctly you can use this code :
private void Update(){
yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
transform.rotation = Quaternion.Euler(pitch, yaw, 0f);
}
doing it that way, when you are walking on a wall, moving the mouse left and right will result in the player camera looking around in a weird way, I want it to be looking around the same way when walking on the floor
Your answer
Follow this Question
Related Questions
Slowly change an objects Z rotation to certain value 2 Answers
How do I reset the rotation? 1 Answer
Help with rotation. 1 Answer
GeoMagic Haptic lock rotation axis 0 Answers
GUI Follow RaycastHit 2 Answers