- Home /
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?
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
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
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
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.
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
Follow this Question
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