Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
6
Question by csciguy · Mar 28, 2011 at 10:08 PM · rotationlookat

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
6
Wiki

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image energyred7 · Jul 10, 2015 at 09:40 PM 0
Share

To mrekuc That doesn't function in any way, shape, or form.

avatar image
4

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);
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image csciguy · Mar 29, 2011 at 12:55 AM 0
Share

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.

avatar image AppleseedSmoothie csciguy · Sep 26, 2018 at 06:08 AM 0
Share

The samething is happening to me too where the its just casually looking at the target and not along the y-axis

avatar image Jesse Anders · Mar 29, 2011 at 02:05 AM 0
Share

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.

avatar image csciguy · Mar 30, 2011 at 11:00 AM 0
Share

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.

avatar image Jesse Anders · Mar 30, 2011 at 11:21 AM 0
Share

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.

avatar image Lenn Dolling · Jul 29, 2011 at 06:01 AM 0
Share

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

Show more comments
avatar image
2

Answer by wicked208 · Apr 26, 2011 at 05:40 PM

http://forum.unity3d.com/threads/36377-transform.LookAt-or-Quaternion.LookRotation-on-1-axis-only

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Justin Warner · Mar 28, 2011 at 10:42 PM 0
Share

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

avatar image csciguy · Mar 29, 2011 at 12:57 AM 0
Share

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.

avatar image Justin Warner · Mar 29, 2011 at 01:13 AM 0
Share

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

avatar image
0

Answer by klesun · Oct 23, 2016 at 05:13 PM

 transform.LookAt (new Vector3(targetPoint.x, transform.position.y, targetPoint.z));
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges