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): using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement; public class Laps : MonoBehaviour { // These Static Variables are accessed in "checkpoint" Script [SerializeField] private string LoadLevel; public Transform[] checkPointArray; public static Transform[] checkpointA; public static int currentCheckpoint = 0; public static int currentLap = 0; public Vector3 startPos; public int Lap; public Text Giri; public Text Fine_Gara; void Start() { startPos = transform.position; currentCheckpoint = 0; currentLap = 0; } void Update() { Lap = currentLap; checkpointA = checkPointArray; Giri.text = "Giri: " + currentLap.ToString(); if (currentLap > 5) { Fine_Gara.text = "Fine Gara"; StartCoroutine(Delay()); } } IEnumerator Delay() { yield return new WaitForSeconds(3f); SceneManager.LoadScene(LoadLevel); } } and for Checkpoint: Code (CSharp): using UnityEngine; using System.Collections; public class Checkpoint : MonoBehaviour { void OnTriggerEnter (Collider other) { //Is it the Player who enters the collider? if (!other.transform.CompareTag("Player")) { return; } if (transform == Laps.checkpointA[Laps.currentCheckpoint].transform) { //Check so we dont exceed our checkpoint quantity if (Laps.currentCheckpoint + 1 < Laps.checkpointA.Length) { //Add to currentLap if currentCheckpoint is 0 if(Laps.currentCheckpoint == 0) Laps.currentLap++; Laps.currentCheckpoint++; } else { //If we dont have any Checkpoints left, go back to 0 Laps.currentCheckpoint = 0; } } } }
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): using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement; public class Laps : MonoBehaviour { // These Static Variables are accessed in "checkpoint" Script [SerializeField] private string LoadLevel; public Transform[] checkPointArray; public static Transform[] checkpointA; public static int currentCheckpoint = 0; public static int currentLap = 0; public Vector3 startPos; public int Lap; public Text Giri; public Text Fine_Gara; void Start() { startPos = transform.position; currentCheckpoint = 0; currentLap = 0; } void Update() { Lap = currentLap; checkpointA = checkPointArray; Giri.text = "Giri: " + currentLap.ToString(); if (currentLap > 5) { Fine_Gara.text = "Fine Gara"; StartCoroutine(Delay()); } } IEnumerator Delay() { yield return new WaitForSeconds(3f); SceneManager.LoadScene(LoadLevel); } void Loss() { StopAllCoroutines(); Fine_Gara.text = "You Failed - Try Again"; StartCoroutine(Delay()); } }
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.
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.
on the Laps script. add public the Loss function public void Loss() this will get you started for the first lap. Code (CSharp): using UnityEngine; using System.Collections; public class Checkpoint : MonoBehaviour { public bool isPlayerFirst = true; // start it as true void OnTriggerEnter (Collider other) { //Is it the Player who enters the collider? if (!other.transform.CompareTag("Player")) { isPlayerFirst = false; return; } if (transform == Laps.checkpointA[Laps.currentCheckpoint].transform) { if(!isPlayerFirst) { //failed to reach this checkpoint first Laps.Loss(); //should not reach this return, but just in case return; } //Check so we dont exceed our checkpoint quantity if (Laps.currentCheckpoint + 1 < Laps.checkpointA.Length) { //Add to currentLap if currentCheckpoint is 0 if(Laps.currentCheckpoint == 0) Laps.currentLap++; Laps.currentCheckpoint++; } else { //If we dont have any Checkpoints left, go back to 0 Laps.currentCheckpoint = 0; } } } }
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.
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.
it still sounds like you didn't change it to static public void Loss () needs to be public static void Loss ()
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?
StartCoroutine and StopAllCoroutines are not going to work from a static method. Not sure why we're going down the static route...
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.
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.
This is how to use the script with out using static public varables. Code (CSharp): using UnityEngine; using System.Collections; public class Checkpoint : MonoBehaviour { private Laps laps; private void Awake() { laps = (Laps)FindObjectOfType(typeof(Laps)); } void OnTriggerEnter(Collider other) { //just incase laps was not set in the Awake if(!laps) { laps = (Laps)FindObjectOfType(typeof(Laps)); } //Is it the Player who enters the collider? if (!other.transform.CompareTag("Player")) { return; } //If this was my project, I would put all these checks in the Laps script //if (transform == Laps.checkpointA[Laps.currentCheckpoint].transform) if(transform == laps.checkpointA[laps.currentCheckpoint].transform) { //Check so we dont exceed our checkpoint quantity if (laps.currentCheckpoint + 1 < laps.checkpointA.Length) { //Add to currentLap if currentCheckpoint is 0 if (laps.currentCheckpoint == 0) laps.currentLap++; laps.currentCheckpoint++; } else { //If we dont have any Checkpoints left, go back to 0 laps.currentCheckpoint = 0; } } //you can remove this, just showing how to call the Loss function. laps.Loss(); } } Code (CSharp): using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement; public class Laps : MonoBehaviour { // These Static Variables are accessed in "checkpoint" Script [SerializeField] private string LoadLevel; public Transform[] checkPointArray; public Transform[] checkpointA; public int currentCheckpoint = 0; public int currentLap = 0; public Vector3 startPos; public int Lap; public Text Giri; public Text Fine_Gara; void Start() { startPos = transform.position; currentCheckpoint = 0; currentLap = 0; } void Update() { Lap = currentLap; checkpointA = checkPointArray; Giri.text = "Giri: " + currentLap.ToString(); if (currentLap > 5) { Fine_Gara.text = "Fine Gara"; StartCoroutine(Delay()); } } IEnumerator Delay() { yield return new WaitForSeconds(3f); SceneManager.LoadScene(LoadLevel); } public void Loss() { StopAllCoroutines(); Fine_Gara.text = "You Failed - Try Again"; StartCoroutine(Delay()); } }
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?
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.