- Home /
Rotate object towards a target on only y axis?
How do i rotate an object towards a target on only y axis?
Answer by Vectrex · Feb 09, 2012 at 02:11 AM
A quick way is to transform.LookAt(target) and then erase the x and z rotations. Assuming you want something like an RTS character than never leans over
void Update ()
{
// Look at including x and z leaning
transform.LookAt(target);
// Euler angles are easier to deal with. You could use Quaternions here also
// C# requires you to set the entire rotation variable. You can't set the individual x and z (UnityScript can), so you make a temp Vec3 and set it back
Vector3 eulerAngles = transform.rotation.eulerAngles;
eulerAngles.x = 0;
eulerAngles.z = 0;
// Set the altered rotation back
transform.rotation = Quaternion.Euler(eulerAngles);
}
Answer by xylax · Feb 08, 2012 at 11:28 AM
Without more info I cannot help you further.
var target : Transform;
// Rotate the Object every frame so it keeps looking at the target
function Update() {
transform.LookAt(target);
}
Dont't forget to mark the question as answered and give a possable thumbs up.
Also you could add constantForce.relativeForce = Vector3(0, 0, 1); to make the object move forward at a constant speed.
sure, you have to explain what you want to do a bit more. Is it like a tire rolling toward something or more like an object pivoting around another object?
i'm trying to create an ai script, i just can't wrap my brains around how to make the enemy face towards the player.
Answer by frozendog_bren · Sep 13, 2016 at 12:12 AM
transform.rotation = Quaternion.LookRotation (target.transform.position - transform.position);
transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
Answer by afshin_a_1 · Apr 23 at 04:55 PM
rotate towards player around world y:
float x = transform.rotation.eulerAngles.x;
float z = transform.rotation.eulerAngles.z;
Quaternion tempRotation = Quaternion.LookRotation(target.transform.position - transform.position);
transform.rotation = Quaternion.Euler(x, tempRotation.eulerAngles.y, z);