So I have a “Laser” components attached to Gameobjects that are being shot at the player. These game objects are moving very slowly so they could be touching the player for at least a couple of seconds.
The Laser component have the OnTriggerEnter,OnTriggerStay, and OnTriggerExit functions in them.
The thing is I want the player to die if a laser is touching the player for a maximum 6 seconds.
*If two lasers are touching the player they will both add to the time. So if two lasers have touched the player for 3 seconds each, the player will die.
*There could be any amount of lasers touching the player at a single moment, but they will all add to the time, and the player will die when the timer reaches 6 seconds.
*If none of the lasers are touching the player I want the timer to go down. Let’s say one laser touched the player for 4.64 seconds, and no other laser is touching the player I want the timer to slowly but surely go back to 0 again. So after 4.64 seconds the timer is back to 0.
I am having problems finding a nice solution(too much if sentences) to this and any help would be very appreciated.
It sounds like the player is keeping track of being hit by the laser. Is the laser adding to the timer or is the player adding to the timer?
We would need to see the code to help out.
I would do a Coroutine for the timer to move back to 0
Create a public int on the player script. Any time a laser touches the player increment it, and any time a laser leaves the player collider decrement it. In the player’s Update() function add something like:
You can just reset the timeInLaser variable to 0 immediately after you leave the laser, or make it count back down (if you’re in the laser for 4 seconds then leave, it will take 4 seconds to return to 0).