Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
0
Question by SVGK · Aug 13, 2014 at 05:34 PM · rotationquaternionpoint

Rotate object to a specific angle and then stop.

I need to be able to define a rotation value in all three axis, have the object rotate to that point over a certain time period (which I also define in a variable), and then have it stop moving once it reaches that point (or gets within 1 or 2 units of the point).

How do I do this?, Transform.Rotate doesn't seem to let me specify a specific point to go to, and I have no idea how to use Quaternions.

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
10
Best Answer

Answer by cloudcamaleoniv · Aug 13, 2014 at 06:13 PM

So, you want to animate the rotation value over time until it reaches a certain value.

Inside the Update method, you can use the Lerp method to keep rotating the object to a point, but you will never really reach this point if you use Lerp. It will keep rotating forever (always closer to the point).

You can use the following:

 private bool rotating = true;

 public void Update()
 {
     if (rotating)
     {
         Vector3 to = new Vector3(20, 20, 20);

         if (Vector3.Distance(transform.eulerAngles, to) > 0.01f)
         {
             transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, to, Time.deltaTime);
         }
         else
         {
             transform.eulerAngles = to;
             rotating = false;
         }
     }
     
 }

So, if the distance between the current object angle and the desired angle is greater than 0.01f, it jumps right to the desired position and stop executing the Lerp method.

Comment
Add comment · Show 2 · 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 SVGK · Aug 14, 2014 at 02:37 PM 0
Share

Ah, thanks mate, works perfectly.

avatar image chetan_dabhi · Feb 23, 2016 at 07:25 AM 0
Share

but image cannot rotate....

avatar image
1

Answer by admin_8C · Jun 13, 2017 at 08:56 AM

I turn again and again..... never stop... doesnt works

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 JDavidL · Jul 30, 2018 at 05:13 PM 0
Share

This works for me... I hope it helps you:

void Update (){ if (rb.transform.rotation.z != (out2 )) StartCoroutine (RotateZell ());
}

IEnumerator RotateZell () { while(rb.transform.rotation.z < out2 || rb.transform.rotation.z > out2) { rb.transform.rotation = Quaternion.Slerp(rb.transform.rotation, Quaternion.Euler(0, 0, out2), 0.1f * Time.time); yield return null; }

     rb.transform.rotation = Quaternion.Euler(0, 0, out2);
     yield return null;
 }

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

25 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

Related Questions

Rotation of object placed in the middle point between two points? 0 Answers

Turning a vector into a local offset? Math help 2 Answers

3rd player orientation using gyro with offset 0 Answers

Easing with Mouse Release 2 Answers

Clarifying the ambiguities of rotation in Unity 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