Search code examples
c#unity-game-enginegame-physics

Unity - make object face the same direction on one axis? Not working?


Ok, I have the Main Camera able to turn on all 4 axes using Google's Magic Window script and the iPhone gyro. I also have a character that exists on the ground and needs to face the same direction as the player, meaning where they are looking.

From what I can see this is the y axis:

enter image description here Neither objects are children of anything. Issue is when I do this to align the character with the y axis of the Camera (the player):

 transform.rotation = new Quaternion(transform.rotation.x, player.transform.rotation.y, transform.rotation.z, transform.rotation.w);

I get nothing. Ive tried other axes but y is supposed to be left/right, and I don't understand whats wrong here.

Do I need to do something with transform.forward? Whats the correct way to do this?


Solution

  • This might not be the best solution but you could use transform.forward, as you mentioned, remove the Y component and transform it into a Quaternion.

    Vector3 rotation = camera.transform.forward;
    rotation.y = 0f;
    
    player.transform.rotation = Quaternion.LookRotation(rotation);