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 Hamesh81 · Nov 02, 2012 at 04:52 PM · transformrotateanglespecificsmoothly

How can I rotate the transform by a specific angle at a certain speed?

I would like to rotate my transform by exactly 180 degrees from its current rotation, but be able to set the speed at which it rotates there. I am using a key input to trigger this, think of it as a "turn around" key to get the character to face the opposite way whenever the key is pressed once (not held down).

At the moment I am using the following Quaternion Lerp:

 hero.rotation = Quaternion.Lerp(hero.rotation, Quaternion.AngleAxis(180, Vector3.up), Time.deltaTime * turnAroundSpeed);

But I always rotate to the same angle, instead of having the angle added onto my current rotation. If I use transform.Rotate the character simply snaps to the angle and I cannot control the speed it rotates at. How could I do this?

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

2 Replies

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

Answer by Eric5h5 · Nov 03, 2012 at 03:07 AM

Use a coroutine like MoveObject:

 MoveObject.use.Rotation (transform, Vector3.up * 180.0, turnAroundTime);

Specifically, the end rotation is essentially achieved like this:

 endRotation = startRotation * Quaternion.Euler (degrees);

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 Hamesh81 · Nov 03, 2012 at 04:25 AM 0
Share

Thanks a lot for your help Eric5h5. So from the information in the link above it seems I would need to attach a separate "$$anonymous$$oveObject" script and then access it from my script using your suggestions above, correct? I also found the following (Add 90 degrees to transform.Rotation) on U.A. which deals with adding specific degrees to the transform, I haven't tested it yet in C# since it's in JS. Would this be a simpler solution?

avatar image Eric5h5 · Nov 03, 2012 at 05:32 AM 0
Share

Yes, I'd suggest putting the $$anonymous$$oveObject script on an empty game object. It's simpler since it's already written, so you just need to call it using one line of code when you want to rotate something.

avatar image Hamesh81 · Nov 04, 2012 at 03:52 PM 0
Share

$$anonymous$$arking this as "Accepted" since it was the first correct answer, even though it wasn't the actual solution I ended up using ;)

avatar image
0

Answer by Hamesh81 · Nov 04, 2012 at 04:08 PM

An alternative one script solution to the above, and the solution I ended up using is the Mathf.MoveTowardsAngle. It takes a few more steps to set up but worked better for me in the end.

Since it works with euler angles it's easy to grab the current euler angle (in my case y) and add 180 degrees (or whatever you want) to it in the update function. Because I wanted the 'turn around' to happen on a button/key press, I had the above calculation made on the GetButtonDown/GetKeyDown, as otherwise the angle calculation wouldn't stop and so neither would the rotation. I had the actual rotation called in GetButton/GetKey with the Mathf.MoveTowardsAngle, after the angle calculation. In order for the character to complete the rotation, the speed variable can be set quite high.

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

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

Transform.Rotate() stuck at 90 and 270 degrees 3 Answers

Transform Rotate a certain angle (and back) 2 Answers

Rotate 90 over time on mouseDown 2 Answers

Calc 30° left and x units forward from local forward axis 1 Answer

Rotate an Object based on shorter angle 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