- Home /
Time.deltaTime or Time.fixedDeltaTime for animated textures, such as a movie without sound (sequenced textures)
What is the best choice? with Time.deltaTime I sometimes get some "timeskips" when adding the value to a variable, in the Update()
:
currentMS += Time.deltaTime * 1000;
The init value is 0 (int), but when adding the currentMS = Time.deltaTime * 1000
(milliseconds), it sometimes skips and adds a high number, it can best be descriped as similar to a frameskip or a fps drop. Can I use FixedUpdate()
with Time.fixedDeltaTime
instead?
The code is based on images (frames) extracted from a movie file. The frames are then inserted into an array. And the code switches frames based on fpms (frames per millisecond).
m = frames per millisecond
f = frames per second
m =(1/f)*1000
My current code is here:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class SVideo : MonoBehaviour {
public Texture2D[] frames;
public bool isPlaying = false;
public int fps = 1;
private int ms = 1000;
private float currentMS = 0f;
private int i = 0;
// Use this for initialization
void Start () {
ms = (int) ((1f / (float) fps) * 1000f);
Debug.Log("FP-MS: " + ms);
}
// Update is called once per frame
void Update () {
if (isPlaying) {
currentMS += Time.deltaTime * 1000;
Debug.Log("Current MS: " + currentMS);
if (currentMS >= ms) {
currentMS = 0f;
if (i >= frames.Length-1) {
i = 0;
}
this.renderer.material.mainTexture =(Texture) frames[i];
i++;
}
}
}
void OnDrawGizmos() {
Gizmos.DrawIcon(this.transform.position, "SVideo.png", true);
}
public void Play() {
isPlaying = true;
audio.Play();
}
}
Just thoroughly re-read the Time.deltaTime reference. And it states this:
When called from inside $$anonymous$$onoBehaviour's FixedUpdate, returns the fixed framerate delta time.
So my guess is that it's best to use FixedUpdate()
with Time.deltaTime
, is my assumption correct?
Answer by robertbu · Oct 01, 2013 at 12:10 AM
You can use either one. FixedUpdate() and Time.fixedDeltaTime or Time.deltaTime will likely give you a bit more regular frame rate. With that said, I do see one bug in your code that might account for what you are seeing. On line 27/28 you have this:
if (currentMS >= ms) {
currentMS = 0f;
A better solution will be:
if (currentMS >= ms) {
currentMS -= ms;
Also here is another implementation for comparison. It uses Update() also, so you check to see if it is smoother.
http://answers.unity3d.com/questions/392517/introduction-movie.html
Your answer
Follow this Question
Related Questions
Unity Movie Texture Audio Black Screen 1 Answer
MovieTexture Quality 0 Answers
Assigning UV Map to model at runtime 0 Answers
Swap Custom Cursor for Animated Movie Texture or Sprite Sheet? 1 Answer
Image Sequence Loop 0 Answers