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 /
avatar image
1
Question by HolidayAtHome · Mar 25, 2013 at 09:13 PM · axislookatrotation axis

lookAt on the x axis, without the 180°turn!

Hi =)

I want to rotate my object only on the x axis looking at a target that rotates around the object's x axis. Sounds easy at first. But the problem now is, after 90° of turning up or down, the object would be upside down, which results in a 180° turn around the y axis at those points. Imagine you are looking at a bee that's flying though you legs. After it's on the other side, you won't having your head keep turning until it's upside down while continue looking at the bee, but you turn 180° when it got behind you. Look at the attached image to see what I mean.

As I searched how to fix that problem peolple said things like

     transform.LookAt(Vector3(0, target.y, target.z));


but then I just got a bit twitching...

The y rotation jumps from 90 to 270 because of that 180° turn, so I tried to make it stay always 90°, which just ended in some weird results....

Making the Objekt a child of another object, which is turned 90° so that it would rotate around the Y axis to look at the target. But the y axis just jumpf out of the parents rotation, having it's y axis looking up in world space again ......

I hope you understand what I mean and have a Idea how to fix that!

alt text

lookat.png (38.3 kB)
Comment
Add comment · Show 3
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 whydoidoit · Mar 25, 2013 at 09:24 PM 0
Share

Check out my tutorial on Quaternions on Unity Gems

avatar image theruins · Apr 01, 2013 at 04:37 PM 1
Share

if you find a solution, please share here. I'm fighting the same problem at the moment! thanks, mate!

avatar image Ubahs · Apr 01, 2013 at 11:26 PM 0
Share

Helping searchability - not an answer: This is called gimbal lock, and it occurs when you're not using Quaternions for your camera's rotation.

Don't ever look into the math behind quaternions, they're magical and just work with, quite seriously, imagination. Well, imaginary numbers and four dimensional math.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Apr 01, 2013 at 04:42 PM

Presuming that in your drawing X is into the screen (weird, but it seems that's what you are saying)

  var targetPosition = target.position;
  var currentPosition = transform.position;
  targetPosition.x = 0;
  currentPosition.x = 0;
  transform.rotation = Quaternion.LookRotation(targetPosition - currentPosition, Vector3.up);
Comment
Add comment · Show 6 · 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 HolidayAtHome · Apr 01, 2013 at 09:09 PM 0
Share

Still the same result ..... Y axis always looking up, which means that the object is turning 180° around Y when I get from one side of the target to the other ... But now I don't have to care about the depht of its position anymore, what helps alot!

There's only 1 problem left now. Atm it turns 180° instantly, what is perfect, so it's no prob with a 2 sided texture. But when I try to add a Quaternion.Slerp to it, I get a slow turn around the Y axis, and no instant switch anymore.... I tried a few ways to add it to the code, but I get either a slow Y turn or just an error ... So if you could put a working Quaternion.Slerp to it, that would be great =)

alt text

12.png (30.6 kB)
avatar image whydoidoit · Apr 01, 2013 at 10:13 PM 0
Share

So hang on - a slerp is going to give you a slow turn - I'm not sure what you want there?

Are you after a smoothed motion on the turn?

avatar image theruins · Apr 02, 2013 at 02:12 PM 0
Share

that's exactly my problem, HolidayAtHome. The green axis should be pointing downwards in the image on the right, but it doesn't. I don't know, but I feel that most people in the forums don't understand the problem. or I don't understand the answers...? I'm watching this topic very intently!:) Thanks!

avatar image whydoidoit · Apr 02, 2013 at 02:17 PM 0
Share

How about trying:

  var rotateRightToUp = Quaternion.FromToRotation(Vector3.right, Vector3.up);
  transform.rotation = Quaternion.LookRotation(targetPosition - currentPosition, Vector3.right) * rotateRightToUp;
avatar image theruins · Apr 03, 2013 at 04:43 AM 0
Share

Thank you so much. I tried it with this and still failed. But still I it to work brilliantly with the SmoothOrbit script shipped with unity. So HolidayAtHome, I recommend using that as a reference or starting point for a new try and never touching lookat or Quaternion.LookRotation ever again;) Not if you want 6DOF, at least. Or again, maybe I just messed up. I miss 2d:( Thank you for your great help though, you kept the hope alive!

Show more comments
avatar image
0

Answer by BeB_Wir3 · Apr 02, 2013 at 08:30 AM

i had the same problem too with a charakter lookin at me, and rotated the model when i was at a higher level. what i used was you make the objects x rotation look at itself, its y rotation at the target, and the z rotation at itself.

in my script it looked like this, but in your case you need to tweak it a little.

             Vector3 new_WayPointPos = new Vector3(nextWaypoint.position.x, transform.position.y, nextWaypoint.position.z);
             Quaternion RotateTo = Quaternion.LookRotation(new_WayPointPos - transform.position);
             transform.rotation = Quaternion.Slerp(transform.rotation, RotateTo, Time.deltaTime * 3);


i used a waypoint as a target.

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 LyanApps · Apr 02, 2013 at 06:15 PM

I may have to update this when I get home, but i think this is exactly what I figured out last night. There is no transition to this code so it may be jerky. My case was I was making a turret on an object face a direction (x,z), but only wanted to rotate on local y even if the parent transform was rotated (eg tank on the side of a hill). Please note this is all from my memory and hasn't been tested.

 GameObject go;//Some GameObject that is always facing up World.up
 ...
 var direction = camera.position - target.position;
 var rel_direction = go.transform.InverseTransformDirection(direction);
 //You may have to switch or invert the parameters to the Atan2 function, but this should lock your rotation around the x-axis
 camera.rotation = Quaternion.Euler(Mathf.Atan2(rel_direction.y, rel_direction.z) * Mathf.Rad2Deg, 0, 0);
 ...
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

15 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

Related Questions

Rotation gameobject 0 Answers

Issue with axis values when importing from 3ds max 1 Answer

LookRotation is Returning Weird Values 0 Answers

Making a specific axis look at an object 2 Answers

aiming a transform on one axis. 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