Youssof
Youssof

Reputation: 1001

Unity set player Y-rotation equal to that of the camera Y-rotaion

I need to set directly the Y-Rotation of the player to be equal to the Camera.main Y-Rotation I have searched a lot but can't find the answer.

I know it is simple but I can't manage it.

Edit: I tried this code

transform.rotation = Quaternion.Euler(Camera.main.transform.eulerAngles);

But it set the rotation of the whole rotation of the object equal to the rotation of the camera X,Y,and Z but what I want is to set only the y rotation.Thanks for help.

Edit2:PlayerImage

I forgot to say whay I am using this in I am making a VR Game so when the player rotates his head the camera rotates successfully.

Note: Why I want to do that? because when my player has to move forward it has to have the same y rotation of the camera.

Upvotes: 0

Views: 2126

Answers (1)

PiotrK
PiotrK

Reputation: 391

That should work:

transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, Camera.main.transform.eulerAngles.y, transform.rotation.eulerAngles.z);

Upvotes: 4

Related Questions