Rotate Rigidbody using eulerAngles.y of Camera
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.
Answer by rickymanalo · May 14, 2020 at 05:35 PM
I found the answer. So, kinda confused, what I needed was to rotate the transform
and not the Rigidbody
. Here's the code I found.
Transform mainCamTransform = Camera.main.transform;
transform.eulerAngles = new Vector3(transform.eulerAngles.x, mainCamTransform.eulerAngles.y, transform.eulerAngles.z);
I thought I needed to rotate the Rigidbody
so that moving and firing projectiles would work fine.
Your answer
Follow this Question
Related Questions
Rotate GameObject based on RigidBody Velocity 1 Answer
Rotations on "picked up" object 0 Answers
Rotate Rigidbody towards target rotation using AddTorque() 0 Answers
i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers
Rotate a ragdoll using forces? 1 Answer