Hello, I am making a car game and I have a raycast enemy detector with a point system that is updated if the enemy touches the player’s raycast, my problem is that when it touches the enemy it adds up many points because it is in the update and I want it to add only one point, no more than one, and make it only when I dodge it.
Don’t do your Raycast in Update.
I expressed myself wrong, I meant that I have done it in a method which is called in update but I don’t know how to add only one point.
private bool hitEnemy = false;
// ------------ Later
bool rayHit = Physics.Raycast( ... );
if(rayHit && !hitEnemy)
{
AddPoint(); // Do whatever
hitEnemy = true;
} else if(!rayHit)
{
hitEnemy = false;
}
ok thanks
It almost works fine, only that it adds infinite points per enemy while I play it and it is not what I want, I only want 1 point per enemy
If you MUST run it in Update, then have a Map<Enemy, float>
Have the key be the enemy that you hit and the float be the time that it was hit.
Set some sort of const cooldown and if the current time is greater than or equal to the stored time + the cooldown then remove the enemy from the Map.
Equally, if you only want them to be hit one time (and never again), do one of the following:
- remove the enemy script (if you have one) or the collider from the enemy so they can’t be hit again
- add a variable inside of an enemy script that’s placed on each enemy, and set it to true when they are hit, if the variable is true then they can’t be hit again
- add the enemy gameobject to a List and crosscheck each future hit against the List to make sure you aren’t hitting the same enemy.
ok thanks I will try some of these things