Search Unity

Repeat track if player loose

Discussion in 'Scripting' started by Silveralby, Jun 10, 2020.

  1. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Hi guys I edited these two old scripts that I found on the net for a racing game. How do you think I can add the condition that if the player loses, he must repeat the same track? Thanks to those who will want to help me.

    For Player:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Laps : MonoBehaviour
    7. {
    8.  
    9.     // These Static Variables are accessed in "checkpoint" Script
    10.     [SerializeField] private string LoadLevel;
    11.     public Transform[] checkPointArray;
    12.     public static Transform[] checkpointA;
    13.     public static int currentCheckpoint = 0;
    14.     public static int currentLap = 0;
    15.     public Vector3 startPos;
    16.     public int Lap;
    17.     public Text Giri;
    18.     public Text Fine_Gara;
    19.  
    20.     void Start()
    21.     {
    22.         startPos = transform.position;
    23.         currentCheckpoint = 0;
    24.         currentLap = 0;
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         Lap = currentLap;
    30.         checkpointA = checkPointArray;
    31.         Giri.text = "Giri: " + currentLap.ToString();
    32.         if (currentLap > 5)
    33.         {
    34.             Fine_Gara.text = "Fine Gara";
    35.             StartCoroutine(Delay());
    36.         }
    37.     }
    38.     IEnumerator Delay()
    39.     {
    40.         yield return new WaitForSeconds(3f);
    41.         SceneManager.LoadScene(LoadLevel);
    42.     }
    43.  
    44. }

    and for Checkpoint:

    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3. public class Checkpoint : MonoBehaviour {
    4.    
    5.      void  OnTriggerEnter (Collider other)
    6.      {
    7.         //Is it the Player who enters the collider?
    8.         if (!other.transform.CompareTag("Player"))
    9.         {
    10.             return;
    11.         }
    12.  
    13.         if (transform == Laps.checkpointA[Laps.currentCheckpoint].transform)
    14.          {
    15.              //Check so we dont exceed our checkpoint quantity
    16.              if (Laps.currentCheckpoint + 1 < Laps.checkpointA.Length)
    17.              {
    18.                  //Add to currentLap if currentCheckpoint is 0
    19.                  if(Laps.currentCheckpoint == 0)
    20.                      Laps.currentLap++;
    21.                  Laps.currentCheckpoint++;
    22.              }
    23.              else
    24.              {
    25.                  //If we dont have any Checkpoints left, go back to 0
    26.                  Laps.currentCheckpoint = 0;
    27.              }
    28.          }
    29.      }
    30. }
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    hello,
    So you have to determine what is a loss. and then run the loss function.
    Like is there an out of bounds area. if the player reaches the out of bounds area. run the Loss() function.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5. public class Laps : MonoBehaviour
    6. {
    7.     // These Static Variables are accessed in "checkpoint" Script
    8.     [SerializeField] private string LoadLevel;
    9.     public Transform[] checkPointArray;
    10.     public static Transform[] checkpointA;
    11.     public static int currentCheckpoint = 0;
    12.     public static int currentLap = 0;
    13.     public Vector3 startPos;
    14.     public int Lap;
    15.     public Text Giri;
    16.     public Text Fine_Gara;
    17.     void Start()
    18.     {
    19.         startPos = transform.position;
    20.         currentCheckpoint = 0;
    21.         currentLap = 0;
    22.     }
    23.     void Update()
    24.     {
    25.         Lap = currentLap;
    26.         checkpointA = checkPointArray;
    27.         Giri.text = "Giri: " + currentLap.ToString();
    28.         if (currentLap > 5)
    29.         {
    30.             Fine_Gara.text = "Fine Gara";
    31.             StartCoroutine(Delay());
    32.         }
    33.     }
    34.     IEnumerator Delay()
    35.     {
    36.         yield return new WaitForSeconds(3f);
    37.         SceneManager.LoadScene(LoadLevel);
    38.     }
    39.     void Loss()
    40.     {
    41.         StopAllCoroutines();
    42.         Fine_Gara.text = "You Failed - Try Again";
    43.         StartCoroutine(Delay());
    44.     }
    45. }
     
  3. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Hi johne5 thanks for the reply. I replaced the script with your edit, but it doesn't work. Basically my idea is to write the next track in the private string LoadLevel but if the Player does not arrive first at the finish he must repeat the current track.
    Cattura.JPG
     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    you're correct, my little function doesn't do a lot.
    So, you need build an if statement.
    When the player reaches the check points, you need to check if the player was first to reach it or not. if not, then run my function. I'll see if I can get some code for you.
     
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    on the Laps script. add public the Loss function
    public void Loss()

    this will get you started for the first lap.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Checkpoint : MonoBehaviour {
    5.  
    6.     public bool isPlayerFirst = true; // start it as true
    7.  
    8.      void  OnTriggerEnter (Collider other)
    9.      {
    10.         //Is it the Player who enters the collider?
    11.         if (!other.transform.CompareTag("Player"))
    12.         {
    13.             isPlayerFirst = false;
    14.             return;
    15.         }
    16.         if (transform == Laps.checkpointA[Laps.currentCheckpoint].transform)
    17.          {
    18.              if(!isPlayerFirst)
    19.              {
    20.                 //failed to reach this checkpoint first
    21.                 Laps.Loss();
    22.                 //should not reach this return,  but just in case                
    23.                 return;
    24.              }
    25.              //Check so we dont exceed our checkpoint quantity
    26.              if (Laps.currentCheckpoint + 1 < Laps.checkpointA.Length)
    27.              {
    28.                  //Add to currentLap if currentCheckpoint is 0
    29.                  if(Laps.currentCheckpoint == 0)
    30.                      Laps.currentLap++;
    31.                  Laps.currentCheckpoint++;
    32.              }
    33.              else
    34.              {
    35.                  //If we dont have any Checkpoints left, go back to 0
    36.                  Laps.currentCheckpoint = 0;
    37.              }
    38.          }
    39.      }
    40. }
     
  6. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Ops, I add public void Loss() in Laps script but in your opinion, what reference is needed in brackets? Visual says, reference to the object is necessary for the non-static method.
    Cattura.JPG
     
  7. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    did you change the Loss function to
    public static void Loss()
     
  8. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Yes in the Laps script I changed how you wrote me "public void Loss ()" but in the "Checkpoint" script in visual studio it tells me that an object reference is needed for the property, method or non-static field "Laps. Loss () ". Sorry for my bad English.
    Cattura.JPG
     
  9. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    it still sounds like you didn't change it to static
    public void Loss ()
    needs to be
    public static void Loss ()
     
  10. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    I figured it was more difficult than what I know in C #. Now Visual gives me the same error but on the coroutine entries and on the entry for the UI. Do you know what it can depend on?
    Cattura.JPG
     
  11. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    StartCoroutine and StopAllCoroutines are not going to work from a static method. Not sure why we're going down the static route...
     
  12. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    you could try to cheat a bit. have the Loss() call another function that then calls the coroutine.

    @PraetorBlue Only going down the static route because he was already going in this direction.
     
  13. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Both the static method and the coroutine are not indispensable for me, just to be able to solve in some way. The "Loss" function makes sense to me but I don't know how to fix the errors.
     
  14. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    This is how to use the script with out using static public varables.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Checkpoint : MonoBehaviour
    4. {
    5.  
    6.     private Laps laps;
    7.  
    8.     private void Awake()
    9.     {
    10.         laps = (Laps)FindObjectOfType(typeof(Laps));
    11.     }
    12.  
    13.     void OnTriggerEnter(Collider other)
    14.     {
    15.         //just incase laps was not set in the Awake
    16.         if(!laps)
    17.         {
    18.             laps = (Laps)FindObjectOfType(typeof(Laps));
    19.         }
    20.  
    21.         //Is it the Player who enters the collider?
    22.         if (!other.transform.CompareTag("Player"))
    23.         {
    24.             return;
    25.         }
    26.  
    27.  
    28.         //If this was my project, I would put all these checks in the Laps script
    29.         //if (transform == Laps.checkpointA[Laps.currentCheckpoint].transform)
    30.         if(transform == laps.checkpointA[laps.currentCheckpoint].transform)
    31.         {
    32.             //Check so we dont exceed our checkpoint quantity
    33.             if (laps.currentCheckpoint + 1 < laps.checkpointA.Length)
    34.             {
    35.                 //Add to currentLap if currentCheckpoint is 0
    36.                 if (laps.currentCheckpoint == 0)
    37.                     laps.currentLap++;
    38.                 laps.currentCheckpoint++;
    39.             }
    40.             else
    41.             {
    42.                 //If we dont have any Checkpoints left, go back to 0
    43.                 laps.currentCheckpoint = 0;
    44.             }
    45.         }
    46.  
    47.         //you can remove this,  just showing how to call the Loss function.
    48.         laps.Loss();
    49.     }
    50. }
    51.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5. public class Laps : MonoBehaviour
    6. {
    7.     // These Static Variables are accessed in "checkpoint" Script
    8.     [SerializeField] private string LoadLevel;
    9.     public Transform[] checkPointArray;
    10.     public Transform[] checkpointA;
    11.     public int currentCheckpoint = 0;
    12.     public int currentLap = 0;
    13.     public Vector3 startPos;
    14.     public int Lap;
    15.     public Text Giri;
    16.     public Text Fine_Gara;
    17.     void Start()
    18.     {
    19.         startPos = transform.position;
    20.         currentCheckpoint = 0;
    21.         currentLap = 0;
    22.     }
    23.     void Update()
    24.     {
    25.         Lap = currentLap;
    26.         checkpointA = checkPointArray;
    27.         Giri.text = "Giri: " + currentLap.ToString();
    28.         if (currentLap > 5)
    29.         {
    30.             Fine_Gara.text = "Fine Gara";
    31.             StartCoroutine(Delay());
    32.         }
    33.     }
    34.     IEnumerator Delay()
    35.     {
    36.         yield return new WaitForSeconds(3f);
    37.         SceneManager.LoadScene(LoadLevel);
    38.     }
    39.     public void Loss()
    40.     {
    41.         StopAllCoroutines();
    42.         Fine_Gara.text = "You Failed - Try Again";
    43.         StartCoroutine(Delay());
    44.     }
    45.  
    46.  
    47. }
     
  15. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Thank you for your patience and if we can successfully modify these 2 scripts they can be useful for anyone who wants to create a racing game. I replaced the two scripts with these modified without the static, I saw that you removed the static method in the CheckpointA transform, then I created a new checkpoint positioned a little before the finish line and inserted in the CheckpointA string. I don't understand why but now when the race starts as soon as the first challenger touches the first checkpoint which is on the finish line, the public void Loss() starts and the race restart. Maybe you have to tell void Loss how many laps there are?
    Cattura.JPG
     
  16. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    You didn't read the code.
    just remove line 48 from the checkpoint script.
     
  17. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    I'm sorry I didn't understand that I had to remove line 48, so it would only remain in the "Laps" script with public void Loss(). I tried the game but now even if the player comes first he repeats the same track. I don't want to disturb you further and I try to think about it.Thank's again johne5.
     
    Last edited: Jun 22, 2020