Search Unity

part of a script ignored for one frame sometimes

Discussion in 'Scripting' started by kairomambo, Sep 18, 2018.

  1. kairomambo

    kairomambo

    Joined:
    Sep 18, 2018
    Posts:
    6
    Hello I've got a bug happening maybe 1/3 of the time and it's been a few weeks I can't figure it out so I tried to solve it but couldn't. A Flag follows Player who must bring it to the Zone to get a Point. When Flag is in the Zone, Player get 1 Point and Flag goes to Empy02 (previously it was a Vector3 getting starting Position during voidStart but it didn't work neither). The problem is that often the Flag keeps following Player instead of going to Empty02 and Player wins Points and the game. Here I tried to put the followingPlayer function in a else() but it didn't change anything neither. Also I used OnTriggerStay because sometimes TriggerEnter wasn't detected.
    If anyone had an idea about what's going wrong I would appreciate it a lot. Here is the code, thanks in advance ! :

    private void OnTriggerStay (Collider col) {
    if (jeu) {
    if (col.gameObject == ZoneOther)
    ///////////////////ZoneOther = zone where to bring this.gameObject to get Points
    {
    this.gameObject.transform.position = empty02.gameObject.transform.position;
    /////empty02 is a starting position for the Flag to go back
    }

    Points += 1;
    scoreString = Points.ToString ();
    scoreTxt.text = scoreString;
    if (Points >= scoreObjectif) {
    Victoire ();
    }
    }/////////end if (col.gameObject == ZoneOther)


    else { if (col.gameObject == PlayerOther) {
    this.gameObject.transform.position = PlayerOther.gameObject.transform.position;
    /////the script is on a Flag so it's carried by the Opponent Player
    }
    }
    }////end if(jeu)
    }


    void Victoire() { jeu = false }
     
    Last edited: Sep 18, 2018
  2. kairomambo

    kairomambo

    Joined:
    Sep 18, 2018
    Posts:
    6
    Code (CSharp):
    1.  
    2. private void OnTriggerStay (Collider col) {
    3.         if (jeu) {
    4.             if (col.gameObject == ZoneOther) {
    5.                 if (Points <= 0f) {this.gameObject.transform.position = empty02.gameObject.transform.position;
    6.                 } else {this.gameObject.transform.position = empty03.gameObject.transform.position;
    7.                 }
    8.                 Points += 1;
    9.                 scoreString = Points.ToString ();
    10.                 scoreTxt.text = scoreString;
    11.                 if (Points >= scoreObjLocal) {
    12.                     Victoire ();
    13.                 }
    14.             }
    15.             else { if (col.gameObject == PlayerOther) {
    16.                     this.gameObject.transform.position = PlayerOther.gameObject.transform.position;
    17.                 }
    18.             }
    19.         }
    20.     }
    21.     void Victoire() {
    22.         jeu = false;
    23.         Instantiate(ScreenVict,Vector3.zero,Quaternion.identity);
    24.         ScreenVict.GetComponentInChildren<Text> ().text = "Victoire" + PlayerOther.name;
    25.         Debug.Log ("Instantiate");
    26.  
    27.     }
     
  3. kairomambo

    kairomambo

    Joined:
    Sep 18, 2018
    Posts:
    6
    I did some more tests and I'm quite sure that the Flag is coming back to the Player after going to the Empty, even if it's in the following frame, when Flag is on Empty and not in a TriggerStay state with Player
     
  4. kairomambo

    kairomambo

    Joined:
    Sep 18, 2018
    Posts:
    6
    Code (CSharp):
    1. if (jeu) {
    2.             if (col.gameObject == PlayerOther) {
    3.                 if (ddebug >= 2f) {
    4.                     this.gameObject.transform.position = PlayerOther.gameObject.transform.position;
    5.                 }
    6.             }
    7.             if (col.gameObject == ZoneOther) {
    8.                 if (Points <= 0f) {this.gameObject.transform.position = empty02.gameObject.transform.position;
    9.                     Debug.Log ("toEmpty02");
    10.                 } else {this.gameObject.transform.position = empty03.gameObject.transform.position;
    11.                     Debug.Log ("toEmpty03");
    12.                 }
    13.                 if (ddebug >= 2f) {
    14.                     Points += 1;
    15.                     Debug.Log (Points);
    16.                     ddebug = 0f;
    17.                 }
    18.                 scoreString = Points.ToString ();
    19.                 scoreTxt.text = scoreString;
    20.                 if (Points >= scoreObjLocal) {
    21.                     Victoire ();
    22.                 }
    23.             }
    24.             if (ddebug <= 1f) {
    25.                 ddebug += 1f;
    26.             }
    27.         }
     
    Last edited: Sep 20, 2018
  5. kairomambo

    kairomambo

    Joined:
    Sep 18, 2018
    Posts:
    6
    I thought about something : maybe because the Trigger detection is called when one of the Objects triggering each other is moving, if I keep making the Player moving it's called too often not to happen even when the Zone trigger is supposed to be the one inspected? Anyway with the "ddebug" I added the problem is happening less often, maybe 1/5 of the time, but still can ruin a bit the game experience. As I'm really a beginner I thought the problem was maybe actually easy to solve for most people here. Do you really have no clue about what could be going wrong? Again thanks in advance !
     
  6. kairomambo

    kairomambo

    Joined:
    Sep 18, 2018
    Posts:
    6