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
1
Question by sdgd · Jan 30, 2013 at 04:40 AM · c#updateframetimedeltatime

why is every 2. frame is weird with Time.deltaTime

ok is this normal if unity 3D is longer time active (not play) when I click Play my graphic get's weird 1. frame stays always at first position and every 2. frame is the correct updated frame

at first I thought I have some leak at all new operators in every frame (creating 1000 new and not deleting them in end) because the bigger that my code got more often I'm getting this

but whenever I restart unity every frame is correctly updated

and today I've just opened unity and was so tired of everything from happening and went to bed and when I came back to try few stuff when I clicked play first time on run I got that weirdness again

ok I've just made an crash test and it does crash as usually I get it but usually it takes a day before unity crashes

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class UnityCrashTest : MonoBehaviour {
     public Rect crash ;
     
     void OnGUI(){
         for (int i = 0; i < 1000; i++) {
             GUI.Button(new Rect (i+80,i+80,50,50),"asdf","Box");
         }
     }
 }

in a minute or 2 the graphics starts jumping but not as often if I have unity longer time running

as around every 2 sec I get jump to original position

but I was watching CPU and Memory and saw that memory was slooowly going up but at 10% memory usage unity couldn't handle any longer and crashed

interesting thing is tho memory didn't go back down (and if I start unity it's like at beginning)

but if I restart PC memory goes back down

unity with this script crashes in around 30 min

sadly I cannot look in to GPU and Graphical memory

so how do I get rid of all new operators or how do I kill/destroy/delete all this new operators? after every frame so I won't have leak

and after doing this if I restart unity around 5 times (with crash) I don't even need to click play any longer and it just crashes in few min I can still click on windows and stuff... but not inside unity like - project play button scripts ... ...

and if I do it this way:

     public Rect crash = new Rect (0,0,80,80);
 
     void OnGUI(){
         for (int i = 0; i < 10000; i++) {
             GUI.Button(crash, "asdf","Box");
         }
     }

I have a bit laggy because it's 10k times but the graphic isn't doing that any longer

and I've figured out I kinda can't update Rect every frame to different values without making new rect

and every new rect helps doing that

EDIT:

figured out that time.deltatime does the trick tho don't know why

if I change my camera from:

 using UnityEngine;
 using System.Collections;
 
 public class CameraControll : MonoBehaviour {
     public float YSensitivity = 100 ;
     private float MouseRotationY ;
 
     void Update() {
         MouseRotationY += (Input.GetAxis("Mouse Y"));
         transform.localEulerAngles = new Vector3 (MouseRotationY * YSensitivity * Time.deltaTime , 0 , 0);
     }
 }

to:

 using UnityEngine;
 using System.Collections;
 
 public class CameraControll : MonoBehaviour {
     public float YSensitivity = 100 ;
     private float MouseRotationY ;
 
     void Update() {
         MouseRotationY += (Input.GetAxis("Mouse Y"));
         transform.localEulerAngles = new Vector3 (MouseRotationY * YSensitivity , 0 , 0);
     }
 }

than camera isn't jumpy any longer every 2. frame

tho this isn't working for transform.position as that never jumps don't know why the difference but that happenes for rotation

and why does that happen?

Comment
Add comment · Show 4
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 MountDoomTeam · Jan 30, 2013 at 05:33 AM 0
Share

what you are saying is very unclear:

frame stays always at 1st position = you mean that the game is jammed that the 1st scene

This would be because the start code is taking a long time.

every 2 frame is updated = the frame is only updated every 2nd frame-

I don't think that is what you mean because then it would be very difficult to see with eyes, and that's not an error that can happen unity stop

Please take your time ask the question again because it's very confusing in fact it's the most confusingquestion that I read in a long time

avatar image sdgd · Jan 30, 2013 at 05:44 AM 0
Share

and yes it's very hard to follow with eyes it's like you see 2 pictures that aint totally same

well try the code I don't know how else to explain

avatar image FL · Feb 10, 2013 at 12:30 PM 0
Share

Take a look in OnGUI and when it's called.

avatar image sdgd · Feb 10, 2013 at 12:33 PM 0
Share

yeah I know that but I'm not makeing GUI changes here so OnGUI isn't called even more than 1 time per frame

I would understand if I would make changes on GUI in run time

or would put some random number inside GUI

1 Reply

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

Answer by Adam-Buckner · Feb 11, 2013 at 09:48 PM

Time.deltaTime is something you use when you are trying to translate a value to be frame rate independent.

Time.deltaTime is the time it took to read the code in your scene and render the previous (last) frame.

If you have a fixed value - like "thrustForce" and you try to apply this every frame:

 public float thrustForce = 10.0f;
 
 void Update () {
    transform.Translate (Vector3.up * thrustForce);
 }

... you should make this "frame rate independent", by multiplying by Time.deltaTime. This way you tell Unity that you want to move the transform 10 meters per second rather than 10 meters per frame. Moving 10 meters per frame could vary depending on machine speed and the content of the scene as each frame could take a different amount of time to render.

Adding Time.deltaTime makes this code frame rate independent:

 public float thrustForce = 10.0f;
 
 void Update () {
    transform.Translate (Vector3.up * thrustForce * Time.deltaTime);
 }

On the other hand, there are values that are not fixed in the same way as thrustForce = 10.0 because they occur within the frame itself, like Mouse Delta, which depends not only on how long the last frame took to render, but how fast the user moved the mouse in that time.

If you look at your code:

 MouseRotationY += (Input.GetAxis("Mouse Y"));

... this is already "frame rate independent" as this is the distance travelled by the mouse for this frame.

If you read here: http://docs.unity3d.com/Documentation/Manual/Input.html you will find that "Mouse X and Mouse Y are mapped to the delta of mouse movement.", or the amount of movement the mouse has done that frame! There is, therefore, no reason to multiply this by Time.deltaTime.

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 sdgd · Feb 12, 2013 at 09:15 AM 0
Share

oh gr8 thanks

and I thought if my code is getting bigger I have more leaks somewhere as it was jumpy more and more not that enourmous big code yet tho around 5-6 pages and mostly not constantly calculated by PC

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

Script Instantiates only one platform? 1 Answer

How to update new frame while another is computing? 0 Answers

How does Time.deltaTime provide smoother physics functions? (Frame rate question) 2 Answers

How to prevent Unity to execute all the code in the first frame. 1 Answer

Simple rigidbody.AddForce only applying once each time key is pressed 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