Rotate an object to look at another object on one axis?
I have two gameobjects in a scene. My base object and a target object. I want the base object to rotate along it's Y axis to face or look at, the target object. How do I constrain the rotation of the base object to just the Y axis?
I have tried a slew of different ways to do this, with no luck.
targetPoint = targetObject.transform.position;
Quaternion targetRotation = Quaternion.LookRotation (targetPoint-transform.position ,Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
I've also tried just using Transform.LookAt(targetPoint) with no luck either.
How can I constrain to just one axis (the Y-axis)? I don't care that the two objects are facing in the same direction, but what I'm trying to do is have the base object look at the target object with a fixed rotation axis. Thanks!
Answer by mrekuc · Jul 17, 2013 at 07:09 PM
In C# this works assuming you have the object tagged as "Player" or change it to what yours is.
using UnityEngine;
using System.Collections;
public class GUIObject : MonoBehaviour
{
private GameObject target;
private Vector3 targetPoint;
private Quaternion targetRotation;
void Start ()
{
target = GameObject.FindWithTag("Player");
}
void Update()
{
targetPoint = new Vector3(target.transform.position.x, transform.position.y, target.transform.position.z) - transform.position;
targetRotation = Quaternion.LookRotation (-targetPoint, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0f);
}
}
I used this to rotate an NGUI label to always point at the player while only rotating in the Y. Attach it to what ever object you want to "look at" the player
To mrekuc That doesn't function in any way, shape, or form.
Answer by Jesse Anders · Mar 28, 2011 at 11:31 PM
This is a 'frequently asked question' :) (It comes up quite frequently on the forums.)
There's more than one solution, but here's a common one (C#, untested):
var targetPosition = targetTransform.position;
targetPosition.y = transform.position.y;
transform.LookAt(targetPosition);
This is not working for me. It is causing the object to rotate on both the X and Y axis. The item is rotating from a flat position and ending up in a near vertical position.
The samething is happening to me too where the its just casually looking at the target and not along the y-axis
The above example assumes y is up, so if another axis is up in your case, you'll need to adjust accordingly. Otherwise, the method posted above should work (provided I got the example right). If it's not working, perhaps you could update your post with your current code, and then post a comment here indicating that the post has been updated.
In my scene, Y is up.
I have two game objects, one called base, one called target. base is in front of target. Both are facing the same direction. I want base to turn around (only on the Y axis) and face the target object. Using the above code provided, the base object rotates on both the X and Y axis. The code is attached to the base object and is 3 lines long. Code is as follows:
Vector3 targetPosition = target.transform.position;
targetPosition.y= transform.position.y;
transform.LookAt(targetPosition);
Thanks.
Unless I'm missing something really obvious, the method described (and your code that's based on it) should work. Is there anything else affecting the transform of the object in question? Are there any game object parent-child relationships involved? If it's still not working, perhaps you could edit your post to include the entire script, and then add a comment here indicating that your original post has been changed.
lets say you did a (* -1) on the Y axis value.. it would make the flip so the Up would be Down.. right?
Answer by Justin Warner · Mar 28, 2011 at 10:41 PM
http://unity3d.com/support/documentation/ScriptReference/Transform.Rotate.html
function Rotate (xAngle : float, yAngle : float, zAngle : float, relativeTo : Space = Space.Self) : void
var y = //Whatever you want the y to be, being the targets y, just put this in the update function (Without the var).
function Update(){ y = //Same thing here; transform.Rotate(0, y, 0, Space.World); }
Something like this should work... Put it on the actual object... Hope it helps =).
I am looking for something to rotate (and stop) at the Target object as described above. This just rotates the item on Y only and does not stop the rotation once the item is facing the target.
Then take out the y = ... in the function Update() and put it in the function Start()... Should work then.
Answer by klesun · Oct 23, 2016 at 05:13 PM
transform.LookAt (new Vector3(targetPoint.x, transform.position.y, targetPoint.z));
Your answer
![](https://koobas.hobune.stream/wayback/20220613115410im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using LookAT causes no rotation at all? 1 Answer
Using a transform.Lookat to set a rotation for a Vector3.lerp. 2 Answers
How to make the front, left, right, or the back of a gameobject look at a target? 1 Answer
Mobile Gyroscope, make Camera always rotating towards zero point using Quaternion 2 Answers
Unity 2D lookat and shoot issues 0 Answers