When a button is pressed, I want to rotate the player where the camera is facing. I used the code below to do it.
rb.rotation = Quaternion.LookRotation(Camera.main.transform.forward, Vector3.up);
But because my camera is slightly above the player’s head while with transform.LookAt(transform.parent)
, the camera’s forward is facing down the ground making the player slightly tilted forward which I don’t want.
Then I thought, because I only want the y rotation of the camera to be applied to the player, I would create a Vector3
that only have the camera’s eulerAngles.y in it. The code I made is below.
Transform mainCamTransform = Camera.main.transform;
Vector3 mainCamYRotation = new Vector3(0f, mainCamTransform.eulerAngles.y, 0f);
rb.rotation = Quaternion.LookRotation(mainCamYRotation, Vector3.up);
The problem with this code is that my expected y rotation didn’t get applied but for some reason, a x rotation is applied to the player.