Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 kukulagames · Apr 25, 2020 at 05:41 AM · timedeltatime

Time.delta Time inconsistant

Ive seen everyone use Time.DeltaTime with a multiplier to have consistent speed with movement or rotation. I have a scene set up where after a button is pressed camera rotates 180 degrees using Quaterion.rotatetowards method and for parameters I am using speed*Time.deltaTime where speed is set by me. I restarted the scene 10 times and 2/10 the rotation was almost instant, 1/10 rotation was very slow and the rest was almost the same. Is there a better way to do this and how come it is not consistent?

Edit: OK here is the code that is attached to camera, to clear things up:

public float Speed;

Update() {

Vector3 target = new Vector3(x,y,z);

var rotation = Quaternion.LookRotation(target - transform.position);

Step = Speed * Time.deltaTime;

transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Step); }

I have this script desabled until A button is pressed. A button triggers this script, camera rotates and after reaching destination it disables itself. Now I thought no matter the framerate it should be at a consistent speed but there are times as I said above where I see inconsistencies.

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 Bunny83 · Apr 25, 2020 at 10:17 AM 0
Share

Without seeing your actual code we can not tell you what you did wrong. You only want to use deltaTime when you perform a linear change over multiple frames. You said "after a button press". This already contradicts the premise that it has to happen over multiple frames. $$anonymous$$y guess is that you only call your code once or you use a while loop within a single frame (which is equal to just setting it to the end result).

5 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by logicandchaos · Apr 25, 2020 at 01:05 PM

Time.deltaTime returns the time it took for the last frame.. frames are not equal length so in order to do an operation smoothly you can multiply values by deltaTime. You could also use a coroutine instead, or invoke repeating, which might be more efficient. I'm not sure why it's inconsistent, it might have to do with how you are using the rotate towards stuff.. I'm not sure.

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 Priyanka-Rajwanshi · Apr 25, 2020 at 03:15 PM

Time.deltaTime is the completion time in seconds since the last frame. It largely depends on fps. As time for different frames is different, Time.deltaTime is always inconsistent per frame. However, using time.deltaTime gives you a smooth linear change over multiple frames.

Take a look at this link for a deeper insight. http://codesaying.com/time-deltatime-in-unity3d/

Would be able to help you more if you could post your code.

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 haolly · Apr 30, 2020 at 12:19 PM

Your destination quaternion in Slerp function will be smaller as time goes, so cache it outside update function

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 Pixel256 · Apr 30, 2020 at 06:03 PM

Starting and end points of Quaternion.Slerp should be different objects form the object to witch the script is attached. "from" and "to" should not change during Update (transform.rotation and rotation in your case change in every frame)
So you can do something like this

 public Transform to;
 public Transform from;
 
 public float step = 0f;
 
 public void Update()
 {
     transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, step);
     step = step + Time.deltaTime;
 }



If you use UI button and you want to avoid disabling and enabling game object you can use coroutine. One way to do it

 IEnumerator Rotate()
 {
     float moveSpeed = 0.01f;
         while(transform.rotation.y < 180)
         {
                 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 20, 0), moveSpeed * Time.time);
                 yield return null;
         }
         transform.rotation = Quaternion.Euler(0, 90, 0);
         yield return null;
 }

 public void StartRotation()
 {
     StartCoroutine(RotateImage());
 }

StartRotation should be connected to button’s OnClick() event.


As I see it:

- speed * Time.deltaTime; is used with Quaternion.RotateTowards. Starting point changes during calls so we need “fix” step over time (in every call we start from different point, slightly rotated object)
For example

 public void Update()
 {
     var step = speed * Time.deltaTime;
     transform.rotation = Quaternion.RotateTowards(transform.rotation, to.rotation, step);
 }


- step + Time.deltaTime; is used with Quaternion.Slerp. Starting point doesn’t change during calls so we need to increase step in every frame (in every call we start from the same point)

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 Cassos · May 01, 2020 at 10:33 AM

To say it easy:

Time.deltaTime is different if you call it in Update() or FixedUpdate()!

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

132 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 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 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 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 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

Time.deltaTime not consistent over time 1 Answer

deltatime glitches 1 Answer

Jump with mathf.lerp problem 2 Answers

Game over scene when the time is up ? 2 Answers

Single Step (pause, resume), or externally clock game loop 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