• 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
0
Question by Chimera3D · Mar 03, 2013 at 10:31 PM · axisslerpeuler anglesone

Euler Angles Rotation Problems

I have a code that is supposed to rotate an object towards another object on one axis, the code works the object is forced to turn more than 180 degrees, as if it can't fully turn around w$$anonymous$$ch is my only problem. I really want to keep the interpolation so that rotation is smooth. There is more to the code but here is the part where all the magic happens.

     rotation = Quaternion.LookRotation(transform.position - target.transform.position);
     transform.rotation = Quaternion.Slerp (transform.rotation, rotation, rotSpeed * Time.deltaTime);
     
     var  eulerAngles = transform.rotation.eulerAngles;
 
     eulerAngles.y = startRot.eulerAngles.y;
     eulerAngles.z = startRot.eulerAngles.z;
     
     // Set the altered rotation back
     transform.rotation = Quaternion.Euler(eulerAngles);

So what I am doing is creating a look rotation then slerping the object between current rotation and desired rotation, there is no problem there, then I get the euler angles of that altered rotation, then the y and z components of the euler angles are set to the same as the rotation at the start of the scene (the default rotation), therefore the other two axes will be reset (I know the documentation says don't alter the axes separately but t$$anonymous$$s does't seem to be the cause of the problem). Then set the rotation back, but only for the y and z axes w$$anonymous$$le the x axis remains the way it was when it was "slerped". Again the problem is that the object stops rotating once the target moves beyond the starting rotation (view), anyone know how to fix t$$anonymous$$s?

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 Lockstep · Mar 04, 2013 at 12:06 AM 0
Share

Transform.Rotate seems to be made for rotating an object around one specific axis. You can check the angle to determin the speed and Vector3.cross to determin the direction.

avatar image robertbu · Mar 04, 2013 at 02:24 AM 0
Share

It is strange that you are confining your rotation to the X axis. Typical games confine the Y rotation, What is the viewpoint of your game? And are their any constraints on the target?

avatar image Chimera3D · Mar 04, 2013 at 03:55 AM 0
Share

With the z axis facing the camera and what exactly do you mean by constraints? Most of the time the target is the player.

1 Reply

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

Answer by robertbu · Mar 04, 2013 at 04:04 AM

If I correctly understand what you are trying to do, here is a bit of alternate code that replaces all the code above and gets you out of getting and setting Euler angles:

 Vector3 pos = transform.position - target.transform.position;
 pos.x = transform.position.x;
 rotation = Quaternion.LookRotation(pos);
 transform.rotation = Quaternion.Slerp (transform.rotation, rotation, rotSpeed * Time.deltaTime);
Comment
Add comment · Show 4 · 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 Chimera3D · Mar 04, 2013 at 04:43 AM 0
Share

The primary problem is that the object rotates on other axes to the point where you can no longer see it (it's a 2d sprite), with the code I had it just can't rotate all the way around for some reason, it gets to a certain point and stops and starts jittering. The code above did not work, the sprite did the full rotation but you couldn't see it (because it's a plane), which if my euler angles code wasn't in my code the same thing would've happened. I think it's because the starting rotation angles are facing one way when the sprite is trying to turn around and face another direction, but I can't figure out how to resolve this.

avatar image robertbu · Mar 04, 2013 at 04:53 AM 1
Share

Consider separating the rotation code and facing problem. You can solve the facing problem in a couple of ways. One way would be to parent the sprite to an empty game object (which gets the rotation code) and then add a sibling sprite that faces the opposite way (i.e. back to back sprites). Another way would be to make the sprite a child of the object that is doing the rotation. Have the sprite check it forward vector (Vector3.Angle()) against the camera's forward vector. If it faces away do a localRotation to flip/rotate the sprite to face the camera.

avatar image Chimera3D · Mar 04, 2013 at 07:41 AM 0
Share

I took your first idea idea and it worked, however I simplified it a bit and instead of having a separate game object I just altered the start rotation variable's x component based on wherever the player's x position was relative to the x position of the object itself. There's is a slight jitter when doing the full rotation (when the rotation is at 90 or 180 degrees) which can be fixed easily. Thanks!

avatar image Lockstep · Mar 04, 2013 at 09:21 PM 0
Share

If the problem is solved, can you please mark the question as answered?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Slerp Z and X axis only 1 Answer

Lookat via Single Axis 0 Answers

Problems with rotating and slerp 0 Answers

can i make a hinge joint that is free in more than one axis? 1 Answer

Rotate object on one axis with slerp 1 Answer


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