x


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!

more ▼

asked Mar 28, 2011 at 10:08 PM

csciguy gravatar image

csciguy
59 3 6 7

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first
more ▼

answered Apr 26, 2011 at 05:40 PM

wicked208 gravatar image

wicked208
34 3 2 5

(comments are locked)
10|3000 characters needed characters left

that works in my object but someone can create an "Quaternion.Slerp" for rotation the my object?

Vector3 targetPosition = target.transform.position;
targetPosition.y= transform.position.y;
transform.LookAt(targetPosition);

i need an quaternion.slerp help me?

more ▼

answered Jan 20, 2013 at 03:22 AM

marcelobr gravatar image

marcelobr
0 21 928 51

You should post this as a new question, not as an answer!

Jan 20, 2013 at 03:24 AM aldonaletto
(comments are locked)
10|3000 characters needed characters left

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);
}
more ▼

answered Mar 28, 2011 at 10:41 PM

Justin Warner gravatar image

Justin Warner
6.7k 44 47 89

Something like this should work... Put it on the actual object... Hope it helps =).

Mar 28, 2011 at 10:42 PM Justin Warner

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.

Mar 29, 2011 at 12:57 AM csciguy

Then take out the y = ... in the function Update() and put it in the function Start()... Should work then.

Mar 29, 2011 at 01:13 AM Justin Warner
(comments are locked)
10|3000 characters needed characters left

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);
more ▼

answered Mar 28, 2011 at 11:31 PM

Jesse Anders gravatar image

Jesse Anders
7.7k 11 25 61

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.

Mar 29, 2011 at 12:55 AM csciguy

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.

Mar 29, 2011 at 02:05 AM Jesse Anders

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.

Mar 30, 2011 at 11:00 AM csciguy

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.

Mar 30, 2011 at 11:21 AM Jesse Anders

lets say you did a (* -1) on the Y axis value.. it would make the flip so the Up would be Down.. right?

Jul 29, 2011 at 06:01 AM Lenn Dolling
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jul 17, 2013 at 07:09 PM

mrekuc gravatar image

mrekuc
1 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3598
x472

asked: Mar 28, 2011 at 10:08 PM

Seen: 40278 times

Last Updated: Jul 17, 2013 at 07:09 PM