- Home /
How to detect for constant collision and then count how long the collision has been happening for?
Putting it simple, I am trying to make a grill, and when a gameobject is "on" (colliding with) the grill I want to give it a cooked rate. I thought maybe I could detect when the collision enters and then start counting and incrementing the cook rate. It would work like: cookRate is less than or equal to 25, it's undercooked. cookRate is greater than or equal to 26, its cooked. cookRate is greater than or equal to 40, it's burnt.
I came up with this but I am not sure if this would work at all.
int _cookRate = 0; private void OnCollisionEnter(Collision collision) { for(int i = 0; i < _cookRate; i++) { _cookRate++; } }
Answer by Cassos · Apr 30, 2020 at 10:46 AM
float time; // The time you collied with something
void OnCollisionEnter(Collision other)
{
time = 0;
}
void OnCollisionStay(Collision other)
{
time += Time.deltaTime;
}
Do you think I would have to put this on all of my game objects that can be cooked?
Okay update: $$anonymous$$ade an item class gonna try figure this one out. Will accept this answer when I've tried it.
Answer by harsh2580 · Apr 30, 2020 at 10:34 AM
Yu can use a bool and timer like this : bool IsColliding = false; float timer = 0f;
void On CollisonEnter { IsColliding = true; }
void On CollisionExit{ IsColliding = false; }
void update{ if(IsColliding ){] timer += Time.deltaTime; // Do Your Stuff Here when Clollising is enable it will run otherwise not } }
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Renderer on object disabled after level reload 1 Answer
Initialising List array for use in a custom Editor 1 Answer
AddTorque no longer working when collider is added 2 Answers