- Home /
Time.DeltaTime question.
Thank you ahead of time for any responses regardless of the outcome.
So I just got through watching some videos, reading some books. All in regards to Unity and Javascript. I jumped in blender made a quick dungeon and some models. Now I get into Unity import my stuff. Throw a character together and start writing its script. Mind you I didn't plan anything and Im a total noob. Anyways to make a long story short... I'm attempting to make a torch, and that torch's life burns out X amount of seconds. Well it goes a bit fast. I've read about Time.DeltaTime but I cant figure it out. I might be tired or just need to take a break. Anyways if someone could let me know how to limit my "torchLightLife" to seconds and not the speed of light!! I would be thankful. See Code below. Oh and If you see anything else wrong let me know lol. IE stuff I shouldn't be doing.
#pragma strict
// this is all CharMovement and timing. as well as attacking etc.
//Character Movement variables
var charMove : int;
var charRotate : int;
var moveTime : float;
var rotTime : float;
var allowMove : boolean = true;
//Torch controls variables
var flickerSpeed : float = 0.7;
var torchLightLife : int = 100;
var torchExist : boolean = true;
var randomizer : float;
function Start () {
}
function Update () {
// Movement fwd and back-----------------------------------------------------------------
if (allowMove)
{
if(Input.GetKey("w"))
{
transform.Translate(0,0,charMove);
print("You Take a step foward");
CharacterMove();
}
}
if (allowMove)
{
if(Input.GetKey("s"))
{
transform.Translate(0,0,-charMove);
print("You Take a step back");
CharacterMove();
}
}
// Rotation left and right -------------------------------------------------------------
if (allowMove)
{
if(Input.GetKey("d"))
{
transform.Rotate(0,charRotate,0);
print("You Turn!");
CharactorRot();
}
}
if (allowMove)
{
if(Input.GetKey("a"))
{
transform.Rotate(0,-charRotate,0);
print("You Turn!");
CharactorRot();
}
}
if(torchExist)
{
TorchFlicker();
}
}
// Character move time pause. This basically stops the player from moving to fast.
function CharacterMove()
{
allowMove = false;
yield WaitForSeconds(moveTime);
allowMove = true;
}
// Character rotation pause. This basically stops the player from rotating to fast.
function CharactorRot()
{
allowMove = false;
yield WaitForSeconds(rotTime);
allowMove = true;
}
// This stops the player from clipping through walls. Otherwise with "translate.Transform" i was able to just ignore collision.
// I also had to create a tag "cameraCollision" and set it on said obj.
function OnTriggerEnter (otherObject:Collider)
{
if (otherObject.gameObject.tag == ("cameraCollision"))
{
transform.Translate (0,0,-1);
allowMove = false;
print ("Touched a Collider!");
yield WaitForSeconds(2);
allowMove = true;
}
// start torch trigger pick up.
if (otherObject.gameObject.tag == ("torchPickupItem"))
{
torchLightLife = (Random.Range(500,5000));
torchExist = true;
light.intensity = 2.0;
Destroy(otherObject);
print ("Touched a Collider!");
}
}
// Start Torch controls here.
function TorchFlicker()
{
if (torchExist)
{
light.intensity = 2.0;
}
else light.intensity = 2.0;
randomizer = Random.Range (1.1, 2.1);
light.intensity = randomizer;
yield WaitForSeconds (flickerSpeed);
TorchDeath();
}
function TorchDeath ()
{
if (torchLightLife > 0)
{
yield WaitForSeconds(1);
torchLightLife--;
}
else TorchDie();
}
function TorchDie()
{
if (torchLightLife <= 0)
{
light.intensity = 0;
torchExist = false;
}
}
Answer by Tarlius · Jun 20, 2013 at 12:46 PM
Your problem is that you are calling TorchFlicker every frame. TorchFlicker will will then wait 0.7 seconds, and call TorchDeath, which will then wait 1 second then reduce its life. Therefore, torchLightLife--;
will be called every frame (or so) after 1.7s.
A better approach would be to do something more like
var torchLife : float = 100f; // Note that its a float, not an int
function ProcessTorch() {
if(!torchExist) return;
torchLife -= Time.deltaTime;
if(torchLife < 0) TorchDie();
}
And add ProcessTorch to the update. Time.deltaTime is the time in seconds unity took to render the last frame. Since update is called once per frame, you can use deltaTime to track time. You could also use Time.time in a similar way to KiraSensei's suggestion, instead of System.DateTime (which will have higher overheads than a simple float).
So I tried your suggestion. The torch life no longer moves at the speed of light but isn't exactly second either.
Question: Why is "return" after "if(!torchExist) also I had to change the ! from the parameter. So took it from being if torchExist == false then, to if torchExist == true then - Time.deltaTime. Also what is the purpose of var torchLife :float = 100f; To be more specific what is the purpose of the "f" behind the float?
Answer by KiraSensei · Jun 20, 2013 at 08:45 AM
For a total noob, your code is quite easily readable, congrats :)
I would suggest you to use System.DateTime like this for example :
// When you pick up the torch :
private var startTime:System.DateTime = System.DateTime.Now;
Then you check when you have to destroy the torch :
// code to put in the Update() method
var timer:float = (System.DateTime.Now - startTime).TotalSeconds;
// maxTime contains the "life" value of the torch, in your example : Random.Range(500,5000)
if (timer < maxTime) torchExist = false;
Im a noob and posted a answer ins$$anonymous$$d of responding. Anyways Ty vm.Im way better at modeling/animation but I kinda dig scripting. Its "creative" in a way. I wanted to learn a full coding language but I like to see results, and why do all the hard crap when Unity does it for me? lol. Thanks again.
I haven't attempted yet just woke up. I will here shortly and let you know the results.
I kept getting errors on the if (timer < maxTime). I will attempt to figure it out later. I have stuff I have to do today. Thank you all for responding. I attempted to upvote but it said I didn't have permission.