# How do I make an object face the same way as the player?

I’m making a game where you can use your imagination to summon things, and the first thing you get to summon is a big cannon. But of course you can’t just have the cannon shoot in random directions, so I need it to face the same direction as the player. I thought this would be easy, but nothing works.
I’ve tried using quaternions, eulers, eulerangles, rotate, accelerators and lerps but none of them have worked.

All I want to do is change the z axis of the cannon to be the same as the y axis on the player.

``````if (Input.GetKeyDown("e"))
{
Vector3 playerPos = transform.position;
Vector3 playerDirection = transform.forward;
Quaternion playerRotation = transform.rotation;
float spawnDistance = 10;

smoke.transform.position = playerPos + playerDirection * spawnDistance;
smoke.Play();
StartCoroutine(SpawnCannon(0f));

IEnumerator SpawnCannon(float waitTime)
{
yield return new WaitForSeconds(waitTime);

cannon.transform.position = smoke.transform.position;
var cannonRot = cannon.transform.rotation;
cannonRot.z = playerRotation.y;
cannon.transform.rotation = cannonRot;
}

}
``````
``````cannon.transform.rotation = Quaternion.LookRotation(transform.up);
``````

Whatever you’re doing with trying to manipulate the individual components of the Quaternion is not correct. You’re confusing Quaternions with Euler angles.

2 Likes

Thanks, I didn’t know there was such a simple way to do it with the LookRotation. Now it does face the same way as the player. But it changes the x component to be 0, when I want it to stay at -90. How can I get it to stay as -90?

Nevermind.

You don’t. The only way to stay sane with rotations is to abandon the idea that euler angles shown in the inspector matter at all. They don’t matter and they are not unique anyway. There are an infinite number of ways to represent any given rotation with euler angles and the editor is just going to pick one.