Hi, I have a problem when I collide with an enemy in my game. If I jump and then hit the enemy, I lose all of my life instantly. Here is the code causing the problem :
function OnControllerColliderHit(hit:ControllerColliderHit){
if(hit.collider.tag == "ennemi"){
PerdreVie();
vie--;
print(vie);
if(vie == 0){
this.enabled = false;
yield WaitForSeconds(3);
Application.LoadLevel(Application.loadedLevel);
}else{
yield WaitForSeconds(2);
}
}
}
It just remove a life in the canvas :
function PerdreVie(){
Debug.Log("perdre vie");
var feuilleActuelle = "feuille_"+vie;
var gererAnimation = GameObject.Find(feuilleActuelle).GetComponent(Animator);
Debug.Log(GameObject.Find(feuilleActuelle));
//GameObject.Find(feuilleActuelle).SetActive(false);
//GameObject.Find(feuilleActuelle).transform.localScale = Vector3(0,0,0);
Debug.Log(feuilleActuelle);
gererAnimation.SetBool("perdre_vie",true);
gererAnimation.SetBool("gagner_vie",false);
//var infostat = feuilleAnim_1.GetCurrentAnimatorStateInfo;
//Debug.Log(infostat);
}
Frames move super fast"Light Speed" so when you have a object collide it last 4-5 frames. thats why its registering its triggering multiple times
Isn’t there a way to prevent this ?
Sorry, for being late. You could make a timer so then it would slow it down.
if hit.colder thing you have
Timer = .2f;
Timer -= Time.DeltaTime * speed of whatever;
if (Timer = 0){
do all your code
}
A timer is the way to do this but not sure this is the most efficient way.