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 justinl · Apr 17, 2013 at 08:56 PM · animationcoroutinetime.deltatimeslerpframe rate

Frame rate independent animation not working with Time.deltaTime or Coroutines

I have a script based animation in C# that uses Quaternion.Slerp(). However, I cannot seem to get it to be frame rate independent no matter what I try.

These are the two methods I've tried which rotate an object based on how much you have moved the mouse. As you can see, if you move the mouse fast to the left/right/up/down, the object would rotate quickly, and then Slerp() would rotate the object back to 0,0,0.

This code is located inside of Update(). Timescale is 1. I test the scene with a frame rate of about 100fps and then 500fps and rotation behaves very differently if I switch between those frame rates.

 float rotateX = InputOSP.GetAxis("Mouse Y") * rotateAmount * Time.deltaTime;
 float rotateY = InputOSP.GetAxis("Mouse X") * rotateAmount * Time.deltaTime;
 Vector3 newRotation = new Vector3(defaultRotation.x - rotateX, defaultRotation.y + rotateY, defaultRotation.z);
 currentItem.transform.localRotation = Quaternion.Slerp(currentItem.transform.localRotation, Quaternion.Euler(newRotation), rotateSpeed * Time.deltaTime);


I've also tried a similar method using a Coroutine that is started from Start() and run as an IEnumerator function, but this also is not framerate independent:

 while(true){
 
     float rotateX = InputOSP.GetAxis("Mouse Y") * rotateAmount;
     float rotateY = InputOSP.GetAxis("Mouse X") * rotateAmount;
     Vector3 newRotation = new Vector3(defaultRotation.x - rotateX, defaultRotation.y + rotateY, defaultRotation.z);
     currentItem.transform.localRotation = Quaternion.Slerp(currentItem.transform.localRotation, Quaternion.Euler(newRotation), rotateSpeed);
     yield return new WaitForSeconds(0.2F);
 }
             

I also desperately tried running it from FixedUpdate() but that also did not give me a framerate independent animation.

Any tips would be greatly appreciated!

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

1 Reply

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

Answer by Eric5h5 · Apr 17, 2013 at 09:33 PM

Mouse input is already framerate-independent; never multiply it by Time.deltaTime, because doing so makes it framerate-dependent.

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 justinl · Apr 17, 2013 at 09:41 PM 0
Share

So that means do not multiply anything by Time.deltaTime in the method using Update() if we are using $$anonymous$$ouse Input. Correct? I want to post back confirmation for others who read this answer. It seems to work fairly good but it does still animate differently between the two frame rates. But it's fairly similar.

avatar image justinl · Apr 17, 2013 at 09:45 PM 0
Share

Also, as a second question, do you have any idea why the Coroutine didn't work? I wasn't multiplying it by time.timeDelta but it still behaved drastically different at different fps.

avatar image Eric5h5 · Apr 17, 2013 at 09:48 PM 0
Share

If you're using mouse input, you shouldn't use Time.deltaTime. For example, if it takes you 1 second to move the mouse 1000 pixels, then you are doing so regardless of the framerate. If you toss Time.deltaTime in there, then you've made the movement rate vary depending on the framerate.

avatar image Eric5h5 · Apr 17, 2013 at 09:52 PM 0
Share

As for the coroutine, is there any reason for using WaitForSeconds? That will result in very choppy movement. Normally you use yield return null to wait a frame.

avatar image justinl · Apr 17, 2013 at 09:54 PM 0
Share

I was using WaitForSeconds to create my own "frame rate" as per this answers.unity3d question: http://answers.unity3d.com/questions/236480/having-trouble-figuring-out-coroutines-in-c.html. Yes it was a bit choppy for sure but I was curious if it would solve my problem so I wanted to try it. (by the way I know I have WaitForSeconds(0.2F) but it's actually 0.02F).

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

12 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

Related Questions

function behaves unexpectedly inside a coroutine. 2 Answers

Yield Wait for Seconds (A little help here) 2 Answers

Can the animation editor create local rotational data? 3 Answers

Animation method/coroutine playable once or loop 0 Answers

How can I stop a parented object from resetting its transform when a parent animation play? 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