Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
20
Question by oliver-jones · Dec 01, 2010 at 07:22 PM · lookrotationy-axis

LookAt To Only Rotate on Y Axis - How?

Hello,

I have a LookAt function on my turret, and right now it works fine, the turret rotates itself to look at its target, but how would I only allow my turret to rotate on the Y axis, right now it also tilts too.

This is my lookat function:

var rotation = Quaternion.LookRotation(target.position - transform.position);
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);

Taken from the SmoothLookAt script (assets)

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

10 Replies

· Add your reply
  • Sort: 
avatar image
88
Best Answer

Answer by Mike 3 · Dec 01, 2010 at 07:34 PM

You just need to make sure you're giving planar positions, i.e.:

var lookPos = target.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
Comment
Add comment · Show 11 · 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 eeveelution8 · Feb 05, 2015 at 01:37 AM 0
Share

I've been looking for the same answer, 1+ for you good sir.

avatar image Dragon972 · Apr 16, 2015 at 12:24 PM 0
Share

Thanks Jesus you help me so much LookPos.y = 0; O$$anonymous$$G i'm crying right now 24 hours of search and you give me the answer :')

avatar image SepM · Aug 22, 2016 at 07:40 PM 1
Share

Just in case anyone wants their right side to face in a direction as opposed to the front, just add transform.Rotate(0,-90,0) afterwards. It helped me in making my fighting game.

avatar image andyRogerKats · Dec 14, 2017 at 08:45 PM 0
Share

Hi! This answer works....Except for if I want to lock on the X or the Z axis. Am I missing something? When I do "lookPos.x = 0" the object will swivel on another axis after about 180 degree turns.

avatar image Juhewe · May 28, 2018 at 10:28 PM 0
Share

I want to rotate around a local axis though? Is there any way to do that?

Show more comments
avatar image
117

Answer by Revolver2k · May 09, 2012 at 08:58 PM

Hi All,

While Michael's above code should work. Manually modifying transform.rotation is not the recommended Unity way to rotate an object.

It is in fact possible to limit the axes which rotate using transform.LookAt(). Please see the following code which limits rotation to the Y axis only:

 Vector3 targetPostition = new Vector3( target.position.x, 
                                        this.transform.position.y, 
                                        target.position.z ) ;
 this.transform.LookAt( targetPostition ) ;

At a basic level the LookAt() function calculates rotational values for the object you want to rotate (transform) based on the positional values of the object you want to look at (target). The key benefit to LookAt() is that it ensures your transform always faces the target, calculating the necessary rotational values accordingly.

All the above code is really saying is: "calculate the rotational values for the transform based on the target's X and Z positions (horizontal plane), but the target's Y position will always be the same as mine." E.g. the same height as me. The resulting calculation forces the transform to face in the direction of the target but believes the target is at the same height of the transform. Thus preventing vertical rotation.

This is my interpretation of it anyway. If anyone can provide further detail I would really welcome it!

[4]: http://answers.unity3d.com/users/5636/oliver-jones.html

Comment
Add comment · Show 11 · 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 SGPascoe · May 11, 2012 at 05:52 PM 0
Share

That is an awesome answer and explanation. Thanks very much!

avatar image afonsolage · Jun 24, 2012 at 05:29 AM 1
Share

This is the best answer!

avatar image ljdp · Jan 29, 2013 at 06:43 PM 0
Share

How would I limit the lookAt to an arbitrary axis? For instance to surface normal the player is stood on.

avatar image reppiz01 · Aug 12, 2013 at 10:19 AM 1
Share

has not worked for me :(

avatar image gloopy · Jan 19, 2014 at 01:33 PM 1
Share
  • for this.

There are many many Questions and answers around this issue and this one is a great answer.

For anyone who is doing 3rd person where character should look at the mouse position like I am doing here is my code which I hope can help!

 public Ray $$anonymous$$ousePosition;
 
     void Update () {
         $$anonymous$$ousePosition = Camera.main.ScreenPointToRay(Input.mousePosition);
 DoLookPlayer($$anonymous$$ousePosition);
 }
 
     void DoLookPlayer(Ray mousePos)
     {
             if (Physics.Raycast(mousePos, out CamRay1Hit, CamRay1Length, CamRay1$$anonymous$$ask.value))
             {
                 Debug.DrawLine (mousePos.origin, CamRay1Hit.point, Color.blue, debugRayLength);
                 Vector3 LookPosition = new Vector3(CamRay1Hit.point.x,this.transform.position.y, CamRay1Hit.point.z);
                 transform.LookAt(LookPosition);        
             }            
     }
Show more comments
avatar image
10

Answer by lodendsg · Jan 03, 2015 at 02:47 AM

Take a look at this (http://answers.unity3d.com/questions/566152/lookat-locked-at-y-axis-but-in-local-space.html)

In summary for those of us that want to rotate for example a turret around its local y axis when said turret can be mounted at an angle in world space the above solution does the trick.

What its doing is similar but projects the point on a plane local to the turret or whatever you are rotating. Below is my implementation for a turret system.

 //Project to plane
         float distanceToPlane = Vector3.Dot(selfTransform.up, position - selfTransform.position);
         Vector3 pointOnPlane = position - (selfTransform.up * distanceToPlane);
 
         selfTransform.LookAt(pointOnPlane, selfTransform.up);
Comment
Add comment · Show 5 · 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 3dpowermax · Mar 12, 2016 at 07:33 AM 0
Share

Thank you , it's work very well

avatar image mauriciofelippe · May 01, 2020 at 04:48 AM 0
Share

Best Answer, thanks.

avatar image UnderShad · Jul 02, 2020 at 08:35 PM 0
Share

Best Answer

avatar image benediktfetko · Nov 09, 2021 at 10:47 PM 0
Share

Thanks. Best answer I could find.

And I know It's been a long time, but how could I change this code for it to work around local X axis (or Z axis)? I somehow can't wrap my head around the math behind your code.

avatar image Athos-Krul · Jun 05 at 10:09 PM 0
Share

Best answer. Really helped.

avatar image
4

Answer by UDN_1fb6f943-d639-43c7-816c-76367e5e91b7 · May 08, 2020 at 11:21 PM

I had similar problem and every answers what i found here arent entirely true, becouse other answers here change X and Z axis too. If you want change only Y axis and do not interfere with others axis, you need do something like this:

 var lookPos = target.transform.position - transform.position;
             Quaternion lookRot = Quaternion.LookRotation(lookPos);
             lookRot.eulerAngles =new Vector3(transform.rotation.eulerAngles.x, lookRot.eulerAngles.y, transform.rotation.eulerAngles.z);
             transform.rotation = Quaternion.Slerp(transform.rotation, lookRot, Time.deltaTime * speedOfrotation);

Now objects change only Y axis and can rotate by physics on other Axes.

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 UaSirideain · May 15, 2020 at 03:24 PM 0
Share

Thank you! Solved my problem, since I need to edit the other axes.

avatar image Tiberius1701 · Mar 12, 2021 at 06:55 PM 0
Share

Thank you! This worked for my turrets perfectly!

avatar image stalwart5033 · May 22 at 03:40 PM 0
Share

I did the same, before that I did some research to see if there is something that fits this situation perfectly, but there seems to be no other way.

avatar image
3

Answer by moodele · Mar 12, 2017 at 06:09 PM

To look at the object and only rotate at y axis:

 void Update() 
 {
     Vector3 lookPos = target.position - transform.position;
     Quaternion lookRot = Quaternion.LookRotation(lookPos, Vector3.up);
     float eulerY = lookRot.eulerAngles.y;
     Quaternion rotation = Quaternion.Euler (0, eulerY, 0);
     transform.rotation = rotation;
 }
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 petroslouca · May 10 at 12:23 PM 0
Share

Thank you, that worked in my case... 3D object real-time facing AR camera! I use AR Foundation and it runs smoothly...

  • 1
  • 2
  • ›

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

44 People are following this question.

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

Related Questions

How many degrees must object turn in y-axis to face target? 1 Answer

how to get my look at script to only work on the Y axis? 1 Answer

LookRotation freaks out.. 2 Answers

More specific Quaternion.LookRotation 1 Answer

Elicopter Gun Looking at a crossair 2 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