Count hits (raycast)

Hi there

Basically what I want to achieve is be able to count how many targets I hit in a time range; Let’s take 10 seconds for example; If I hit 10 targets, //do stuff like play a sound or report an achievement; If I hit 5 targets - // do something else like play another sound - and so on.

my hit code looks like this:

function checkHit()
	var hit:RaycastHit;
    if ( Physics.Raycast(transform.position, Vector3.forward * -1, hit) ) 
        if ( hit.collider.tag == "Target" ) // do the stuff

The problem is that I don’t understand how to initiate a timer and how to check how many targets I hit in x seconds. ANy help / link / docs link is appreciated.

When you start the timer store Time.time in a float variable. let’s say you call it startTime.

In Update() the current elapsed time will be Time.time - startTime, you could check whether it has reached 10 and the do whatever you like, based on th number of targets hit. you could then reset a hit counter.

In your function listed you would just add to the counter to count the targets.