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 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;
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 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;
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 wicked208
Apr 26, 2011 at 05:40 PM
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.
public class GUIObject : MonoBehaviour
private GameObject target;
private Vector3 targetPoint;
private Quaternion targetRotation;
void Start ()
target = GameObject.FindWithTag("Player");
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 marcelobr
Jan 20, 2013 at 03:22 AM
that works in my object but someone can create an "Quaternion.Slerp" for rotation the my object?
Vector3 targetPosition = target.transform.position;
i need an quaternion.slerp help me?
You should post this as a new question, not as an answer!
Answer by Justin Warner
Mar 28, 2011 at 10:41 PM
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).
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.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
8 People are following this question.
2d Turret WORKS but is turned 90 degrees from the right way
Trouble with player lookat
How to object rotate on a single axis towards another object? C#
rotation problem (me lio con rotacion)
How to use Quaternion.slerp and Quaternion.LookRotation with a child gameobject