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 Tyler 2 · Jan 17, 2011 at 11:48 PM · movementrotate

Rotation animation?

Hello. This is my script for the rotation of my object. Currently it instantly goes to the random degree, how would I modify my script to make it animate the rotation? Thanks

function Start(){ InvokeRepeating("FR", 0,1); }

function FR (){ transform.Rotate (Vector3.up, Random.Range(90.0, 270.0)); }

Comment
Add comment · Show 1
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 yoyo · Jan 17, 2011 at 11:51 PM 0
Share

See my answer to your previous question ... http://answers.unity3d.com/questions/36915/looped-rotation

3 Replies

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

Answer by DaveA · Jan 17, 2011 at 11:50 PM

Look at the docs for RotateTo and Slerp

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

Answer by _Petroz · Jan 18, 2011 at 01:16 AM

Do something like this:

var finalRotation : Quaternion;

function Start() { finalRotation = Quaternion.AngleAxis(Random.Range(90.0, 270.0), Vector3.up); InvokeRepeating("FR", 0, 1); }

function FR() { transform.rotatation = Quaternion.Slerp(transform.rotatation, finalRotation, Time.deltaTime); }

Links:

http://unity3d.com/support/documentation/ScriptReference/Quaternion.AngleAxis.html

http://unity3d.com/support/documentation/ScriptReference/Quaternion.Slerp.html

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

Answer by Peter G · Jan 18, 2011 at 01:18 AM

I looked at some of your recent questions on rotating, and I'm very confused as to what you want to do, each one is asking something that would not let you do another. With that being said, I'll take a shot answering your question.

It depends on how you want it to rotate: at a set speed or always reaching the goal. You could have a set rotation speed that will always be constant, but only might reach your target angle in a single second, or it might have to wait for most of the second because the angle was very close to its current angle.

/*------------------------------------------------------------ This will cause the object to rotate at a constant speed. We call RandomRotate (FR was confusing for me, feel free to change it back). FR chooses a random angle. Then we create a quaternion using that angle. Then we rotate using Quaternion.RotateTowards() at a constant speed until repeatTime seconds have passed.

Then we pick a new angle and start rotating towards that. ------------------------------------------------------------*/

var repeatTime : float = 1.0; var rotateSpeed : float = 45;

function Start () { for(;;) { RandomRotate(); yield WaitForSeconds(repeatTime); } }

function RandomRotate () { var goal : float = Random.Range(90.0, 270.0); var desiredAngle : Quaternion = Quaternion.AngleAxis(goal, Vector3.up);

 var i : float = 0.0;   
 while(i < repeatTime) {
     transform.rotation = Quaternion.RotateTowards(transform.rotation, desiredAngle, rotateSpeed * Time.deltaTime);

     yield;
     i += Time.deltaTime;
 }

}

The other option would be interpolating (lerp and slerp) which would cause your object to rotate faster the further apart two angles are, but it will always arrive just in time.

/*------------------------------------------------------------ This will cause the object to rotate to a random point in the allotted amount of time. Slerp spherically interpolates the rotation from one to the other based on the parameter i. In this case, i is 0 at the beginning, and 1 after repeatTime seconds, meaning that it will start at one and finish at the other, give or take a small amount of error given Time.deltaTime won't ever be exactly repeatTime seconds.

Then we pick a new angle and start rotating towards that. ------------------------------------------------------------*/

var repeatTime : float = 1.0;

function Start () { for(;;) { RandomRotate(); yield WaitForSeconds(repeatTime); } }

function RandomRotate () { var goal : float = Random.Range(90.0, 270.0); var desiredAngle : Quaternion = Quaternion.AngleAxis(goal, Vector3.up);

 var startRotation : Quaternion = transform.rotation;
 //If we don't find a start point, we will get an easing effect at the end.

 var i : float = 0.0;   
 while(i < repeatTime) {
     transform.rotation = Quaternion.Slerp(startRotation, desiredAngle, i / repeatTime);

     yield;
     i += Time.deltaTime;
 }

}

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

No one has followed this question yet.

Related Questions

Rotate Character with movement 3 Answers

Updating rotation of client not working - strange error 0 Answers

Calling functions in Javascript 3 Answers

Help with Grid Movement 2 Answers

Navigating the 3D platform 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