player reset added

This commit is contained in:
2024-04-26 14:28:40 +02:00
parent a17999b57f
commit 42e007ae51
3 changed files with 37 additions and 2 deletions

View File

@@ -16,6 +16,9 @@ public class PlayerController : MonoBehaviour
public Rigidbody rigidBody;
[HideInInspector] public int checkpointsCollected = 0;
public GameObject[] checkpoints;
Vector3 startPosition;
Quaternion startRotation;
KeyCode resetCarKey = KeyCode.Backspace;
// Start is called before the first frame update
void Start()
@@ -24,6 +27,9 @@ public class PlayerController : MonoBehaviour
{
rigidBody = GetComponent<Rigidbody> ();
}
startPosition = transform.position;
startRotation = transform.rotation;
// Find all child GameObjects that have the WheelControl script attached
@@ -128,6 +134,35 @@ public class PlayerController : MonoBehaviour
float checkpintDistance = distanceToCheckpoint(currentCheckpoint);
// send back to previous checkpoint if stuck
if (Input.GetKeyDown(resetCarKey))
{
if (checkpointsCollected == 0)
{
transform.position = startPosition;
transform.rotation = startRotation;
}
else
{
transform.position = new Vector3(
checkpoints[checkpointsCollected - 1].transform.position.x,
transform.position.y + 3,
checkpoints[checkpointsCollected - 1].transform.position.z
);
transform.eulerAngles = new Vector3(
transform.eulerAngles.x,
checkpoints[checkpointsCollected - 1].transform.eulerAngles.y,
transform.eulerAngles.z
);
}
}
if (checkpintDistance < 0.2f)
{
checkpointsCollected += 1;