So, I’m starting to play with Unity3D. I’m trying to make a double shot for my “Space Shooter” project.
Anyway, I’ve set a script for the bullets that have a function OnTriggerEnter. The bullet prefab is set to isTrigger and everything is working properly.
There’s a litte problem though: The function makes a call to a function that updates the score. And, because the two bullets hits the object at the same time, it gets called twice.
I’ve searched for a solution and I couldn’t find anything that worked. I could make a variable “numberOfHits” and make decreases on it, but I think it’s not a good solution.
Here’s my script:
private var isCollision : boolean = false;
function OnTriggerEnter(other : Collider){
//Check for astroid
if(other.gameObject.tag == "enemy" && !isCollision){
isCollision = true;
//Reset the position of the enemy
other.GetComponent(scriptAstroid).ResetEnemy();
//Create the explosion on impact
if(explosion){
Instantiate(explosion, transform.position, transform.rotation);
audio.PlayClipAtPoint(fxSound, transform.position);
}
//Tell scene manager that we destroyed an enemy and add a point to the score
sceneManager.transform.GetComponent(scriptSceneManager).AddScore();
//Destroy the bullet
Destroy(gameObject);
}
}
On the update function, I reset the isCollision variable to false again. But, because the two bullets hits the enemy AT THE SAME FRAME, it doesn’t matter.
What do you guys suggest? Thanks in advance.