Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Collision triggered multiple time when jumping

Discussion in 'Scripting' started by Styxwar, Oct 16, 2015.

  1. Styxwar

    Styxwar

    Joined:
    Apr 13, 2013
    Posts:
    3
    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 :

    Code (JavaScript):
    1. function OnControllerColliderHit(hit:ControllerColliderHit){
    2.     if(hit.collider.tag == "ennemi"){
    3.         PerdreVie();
    4.         vie--;
    5.         print(vie);
    6.         if(vie == 0){
    7.             this.enabled = false;
    8.             yield WaitForSeconds(3);
    9.             Application.LoadLevel(Application.loadedLevel);
    10.         }else{
    11.             yield WaitForSeconds(2);
    12.         }
    13.     }
    14. }
     
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    what does PerdreVie();
     
  3. Styxwar

    Styxwar

    Joined:
    Apr 13, 2013
    Posts:
    3
    It just remove a life in the canvas :

    Code (JavaScript):
    1. function PerdreVie(){
    2.     Debug.Log("perdre vie");
    3.     var feuilleActuelle = "feuille_"+vie;
    4.     var gererAnimation = GameObject.Find(feuilleActuelle).GetComponent(Animator);
    5.     Debug.Log(GameObject.Find(feuilleActuelle));
    6.     //GameObject.Find(feuilleActuelle).SetActive(false);
    7.     //GameObject.Find(feuilleActuelle).transform.localScale = Vector3(0,0,0);
    8.     Debug.Log(feuilleActuelle);
    9.    
    10.     gererAnimation.SetBool("perdre_vie",true);
    11.     gererAnimation.SetBool("gagner_vie",false);
    12.     //var infostat = feuilleAnim_1.GetCurrentAnimatorStateInfo;
    13.     //Debug.Log(infostat);
    14.    
    15. }
     
  4. GoofBall101

    GoofBall101

    Joined:
    Jul 25, 2015
    Posts:
    57
    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
     
  5. Styxwar

    Styxwar

    Joined:
    Apr 13, 2013
    Posts:
    3
    Isn't there a way to prevent this ?
     
  6. GoofBall101

    GoofBall101

    Joined:
    Jul 25, 2015
    Posts:
    57
    Sorry, for being late. You could make a timer so then it would slow it down.
    Code (csharp):
    1.  
    2. if hit.colder thing you have
    3.  
    4. Timer = .2f;
    5.  
    6. Timer -= Time.DeltaTime * speed of whatever;
    7.  
    8. if (Timer = 0){
    9. do all your code
    10.  
    11. }
    12.  
    A timer is the way to do this but not sure this is the most efficient way.