- Home /
How to get JUST the y rotation of an object?
I'm making a 3rd person game with the controls set up so that:
If the player holds down forward on the left stick (i.e. runs forward) and rotates the camera left/right, he'll run the way the camera's facing.
HOWEVER, my problem is that he is also running the way the camera is facing, rotated around the x/z axis too, so he's turning and facing the sky rather than just running on the x,z plane.
I'm not familiar with matricies, quaternions, or eulr's, so is there a way I can make my character ONLY rotate around the y axis (so that he only runs on the x,z plane), not all 3?
the code I'm using is: "transform.rotation = cameraParent.transform.rotation;", I'm just not sure how to split it up so that it only changes the rotation around the y axis since transform.rotation.y only returns a value and can not be set by calling that
Lots of ways to solve. Here is an approach using Vectors rather than angles:
var dir = cameraParent.transform.forward;
dir.y = 0.0;
transform.rotation = Quaternion.LookRotation(dir);
Answer by highpockets · Mar 10, 2014 at 09:18 PM
You could just get the euler y angle of the camera:
float yRotation = cameraParent.transform.eulerAngles.y;
Then assign it to the transform:
transform.eulerAngles = new Vector3( transform.eulerAngles.x, yRotation, transform.eulerAngles.z );
Answer by goibon · Mar 10, 2014 at 09:14 PM
You only wish to access
cameraParent.transform.rotation.y
but you can't modify that property by itself, so you need to store it in a temporary variable and then apply the rotation like this:
Vector3 newRotation = transform.rotation;
newRotation.y = cameraParent.transform.rotation.y;
transform.rotation = newRotation;
The problem is that transform.rotation is a Quaternion, NOT a Vector 3, so this won't work
Answer by nicolasjr · Mar 10, 2014 at 09:13 PM
Should be something like:
transform.rotation.eulerAngles = new Vector3(transform.rotation.eulerAngles.x, newRotationValue, transform.rotation.eulerAngles.z);
This was close to the solution, but gave an error message because it's supposed to be transform.eulerAngles, no "rotation"
Your answer
![](https://koobas.hobune.stream/wayback/20220613135916im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make the character move and rotate where the camera is facing (3rd person) 0 Answers
transform.rotation and localRotation automatically reset after rotating. 1 Answer
3rd person controller , movement issues 1 Answer
Using gamepad triggers to control 3rd person camera 2 Answers
How to move player in direction where the camera is aiming ? 1 Answer