How to measure rate of Raycast hit events?

hi folks i have a dilemma and just looking for some newbie friendly advice on this. i want to use the measurement of raycast hit events to control music.

i have a a situation where i’m using a Raycast sent from the FPP object in order to see if it detects a robot. i have the various surfaces tagged so that it prints an event depending on which type of object was hit within its radius. so far so good. it generally works reasonably well. now, what i want to do it measure the frequency of events received so that the more events are received the more intense the music is and the lesser number received, the less intense the music is. here’s the current raycast code. it’s largely snarfed from another Unity Answers question:

player = GameObject.Find("FirstPerson Player");
var hit : RaycastHit;
var fwd = player.transform.TransformDirection (Vector3.forward);
    if (Physics.Raycast (player.transform.position, fwd, hit, 30.0)) {
       print("hit something!");
       print(hit.collider.tag);
       if(hit.collider.tag == "Wall"){
 print("hitting a wall");} 
 if(hit.collider.tag == "Robot"){
 print("hitting a Robot!"); 
 targetObject = GameObject.Find("audioTest");
 deaths=1;
 SendMessage ("TensionMusic", deaths);} 
 }

so are we talking time.deltaTime or what function? i’ve looked through the Unity scripting stuff, but maybe it’s a C# syntax function question? i don’t care about elapsed time since the beginning of the game or network time. just to measure time difference between the hits. i could also count the number of hits in say, 5 seconds, but i’m not sure how to do that either.

i have looked around and i haven’t found anything matching my search terms, so if anyone finds a forum post or Answers that deals with this or would care to point me in the right direction i’d be very grateful!

well, to count the number of events in a set period, i use a simple function:

function CountHits(){
hitCount++;
yield WaitForSeconds(5);
hitCount--;
}

but you need to keep in mind that every frame you’re facing say, the wall, the raycast is hitting it, so you probably want to store the hit object to check against the current hit:

if (Physics.Raycast (player.transform.position, fwd, hit, 30.0)) {
if(hit != lastHit){
CountHits();
print("hit something!");
print(hit.collider.tag);
//...etc
lastHit = hit;
}

or something…

thanks seth - this gets me started i think, though i’m a bit curious what the hitcount-- does after the yield statement. based on my (admittedly imperfect) knowledge of Unity scripting it seems your CountHits() will increase until the 5 seconds is up and then it hits the decrease? i guess i’d want to reset the count every 5 seconds - let me see if i can adapt this. would this work?

function CountHits(){
hitCount++;
yield WaitForSeconds(5);
//store count after 5 seconds?
hitCount=StoredRate
//reset count? 
hitCount=0;
}

if i’m wrong and the function indicates what happens in each frame then it means each frame would increment upwards wait 5 seconds then decrement, but that doesn’t seem useful.

i’m mainly interested the number of times per second (or whatever time) i get the hit on the robot object, so it doesn’t have to be a raycast hit per se - i could count messages and basically ignore any other types of hits (like on the wall). so maybe adapt the latter half…

if (Physics.Raycast (player.transform.position, fwd, hit, 30.0)) {
if(hit != lastHit){

so what you’re doing here is filtering out repeats of the same tag? i’d definitely want repeated events but just sorted out, so maybe something like this?

if(hit.collider.tag == "Robot"){
CountHits();
print(StoredRate);
}
//...etc
lastHit = hit;
}