Hey guys.
I am wondering what the best way would be to check how long a ray is hitting an object. Basically I want to fire some logic, but only after a ray has collided with an object for X seconds.
Thanks!
Hey guys.
I am wondering what the best way would be to check how long a ray is hitting an object. Basically I want to fire some logic, but only after a ray has collided with an object for X seconds.
Thanks!
Raycasts live from frame to frame they don’t have a concept of time, you have to tally(is that the right spelling?, count) them yourself.
Keep a variable ‘start’ when the first one hits, something like this
float hitTime;
float maxTime;
Collider col;
void Foo(){
Raycast hit;
if(Physics.Raycast( do your ray, out hit)){
if( hit.collider == col){ // this is the same collider from last frame
if(startTime + maxTime > Time.time){//check if time has passed, meaning we were pointing at collider col for maxTime
//Do w/e
}
}else{
startTime = Time.time;
col = hit.collider;
}
} else{
startTime = 0.0f;
col = null;
}
}
Oh shit it ain’t formatted, oh well… :shrug: