How do I make an object look at an angle in a top-down persepective?

by g_b   Last Updated October 19, 2019 08:13 AM

Given a top-down persepective and lets say I know the angle I want to look at, how do I make a gameobject look into that angle? I currently have code like this:

if (directionX != 0 && directionZ != 0)
{
    transform.rotation = Quaternion.LookRotation(new Vector3(directionX / 2, 0, directionZ / 2));
}
else if (directionX != 0 || directionZ != 0)
{
    transform.rotation = Quaternion.LookRotation(new Vector3(directionX, 0, directionZ));
}

which allows me to move look into 8-directions. I am now looking to make it so I can look in any angle and I've got the angle using:

var angle = Mathf.Atan2(directionZ, directionX) * Mathf.Rad2Deg;

but I don't know how to use it to make my object look into that angle.

Tags : unity


Related Questions


Updated July 02, 2019 11:13 AM

Updated July 12, 2015 13:05 PM

Updated July 13, 2015 13:05 PM