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;
    }

}

For a total noob, your code is quite easily readable, congrats :slight_smile:

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;

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).