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
0
Question by alexanderameye · Jan 10, 2015 at 06:53 PM · rotationrotate360-degreesdegrees

What is the most efficient way to rotate an object?

Let's say I have an object with it's rotation center right on the end (see picture)

alt text

When I rotate it 90 degrees, it will obviously rotate 90 degrees clockwise, but when I set the rotation to 270 degrees, it will go 270 degrees clockwise. How can I make it so that whenever the rotation exceeds 180 degrees, the object will rotate counter clockwise instead of clock wise, so that wen I put in a rotation of 270 degrees, instead of going 270 degrees clockwise, it will go 90 degrees counter clockwise.

Thank you very much!!!

screen shot 2015-01-10 at 19.52.08.png (346.0 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Owen-Reynolds · Jan 11, 2015 at 04:40 PM

A Unity trick is to just specify the final angle, and use one of Quaternion RotateTowards or Lerp/Slerp (which homer3 suggests.) RotateTowards is the simplest (you give the actual amount of degrees to rotate this frame, ex: 90*Time.deltaTime to go a quarter-turn per second,) unless you've already used Lerp lots for movement.

They always take the best 3D path. Sometimes that can surprise you, as they find a better way to flip than the one you really wanted. But if everything is 2D, they work fine.

If you want them to go 180 one way, and they want the other, or you want a 181+ degree rotation, you can fool them by making a midpoint (rotate to this 90 degrees, now this 91 degrees more.)


The other problem is purely coding. Any wrong spins are because you programmed it wrong. If you have your own rotation number, then of course moving it from 0 to 270 is clockwise. So don't. Move it from 0 to -90 or from 360 to 270, or gradually add -90. The Mathf library has some helpful functions: MoveTowardsAngle and DeltaAngle.

A trick with moving your own angles is not to read from rotation.eulerAngle. It can "randomly" add or subtract 360, and in 3D stuff, can even flip +/-180. Just always keep your personal float spin; variable, and use that to set rotation=Quaternion,Euler(0,spin,0);

Generally, the Quaternion shortest-path is fine for when it's really just a pretty animation. The more complicated "spin your own variable" is needed when the rotation direction means something (like a clock face where you want to show time going forwards or backwards,) or need to specify things like 2.5 spins.

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 homer_3 · Jan 10, 2015 at 07:03 PM

Rotations are mathematical operations. Added 2 + 3 isn't going to me any faster than adding 6 + 2.

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 alexanderameye · Jan 10, 2015 at 07:06 PM 0
Share

It's not about how fast the objects rotates, but I want that the object goes left if the new position is at 181 degrees and that it goes right if the new position is 179 degrees (it's for the opening of a door)

avatar image homer_3 · Jan 10, 2015 at 07:50 PM 0
Share

The easiest thing to do is deter$$anonymous$$e which way you want to rotate and then pass in a negative or positive rotation based on that.

avatar image alexanderameye · Jan 10, 2015 at 07:51 PM 0
Share

So if I say like a rotation to -90 degrees, Unity reads that as a rotation to 270 degrees counter clockwise?

avatar image homer_3 · Jan 10, 2015 at 08:50 PM 1
Share

Forget what I said before. I think I just ran into the same issue you are trying to solve. If you are trying to animate the rotation of an object you want to do

 xform.rotation = Quaternion.Slerp( xform.rotation, Quaternion.Euler(targetRotation), rotateSpeed * Time.deltaTime);

This will cause the object to take the shortest rotation path to the target rotation.

avatar image alexanderameye · Jan 10, 2015 at 08:54 PM 0
Share

well, I don't know the math behind quaternions, but it's definitely something I will look into. So the code you gave me is basically this?

object.rotation = Slerp(original rotation, new rotation, speed * Time.deltaTime),

avatar image
0

Answer by Jignesh G. · Jan 16, 2015 at 07:39 PM

http://docs.unity3d.com/ScriptReference/Transform-localEulerAngles.html

Only localEulerAngles to read and set the angles to absolute values. Don't increment them, as it will fail when the angle exceeds 360 degrees. Use Transform.Rotate instead.

So it means Transform.Rotate is most efficient way to rotate object. you can also use Space.Self or Space.World according to the requirement.

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

28 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

Related Questions

Rotation: rotate by a defined angle 1 Answer

Problem with "if or" statement. 1 Answer

How to rotate about 360 on a coroutine 3 Answers

How can I rotate an object with certain degrees? 2 Answers

Rotate object in 45 degree steps 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