Hey there,
Try this if you haven’t already.
Step-by-Step Breakdown:
- Set up your spawn points:
In your scene, create several empty GameObjects where you want the checkpoint to appear.
Name them something like SpawnPoint_1, SpawnPoint_2, etc.
Group them all under an empty parent GameObject called CheckpointManager (for clarity).
- Create a script called CheckpointManager.cs:
using UnityEngine;
public class CheckpointManager : MonoBehaviour
{
public Transform spawnPoints;
public GameObject checkpointPrefab;
private GameObject currentCheckpoint;
void Start()
{
SpawnCheckpoint();
}
public void SpawnCheckpoint()
{
if (currentCheckpoint != null)
{
Destroy(currentCheckpoint);
}
int index = Random.Range(0, spawnPoints.Length);
Transform spawn = spawnPoints[index];
currentCheckpoint = Instantiate(checkpointPrefab, spawn.position, Quaternion.identity);
currentCheckpoint.GetComponent<Checkpoint>().manager = this;
}
}
- Create a script called Checkpoint.cs:
using UnityEngine;
public class Checkpoint : MonoBehaviour
{
public CheckpointManager manager;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
ScoreManager.Instance.AddPoint();
manager.SpawnCheckpoint();
}
}
}
- Tag your player GameObject with “Player”.
Make sure the checkpoint prefab has a Collider set to “Is Trigger” and that it’s assigned in the CheckpointManager inspector, along with the array of spawn points.
- (Optional) Simple ScoreManager.cs if you don’t have one yet:
public class ScoreManager : MonoBehaviour
{
public static ScoreManager Instance;
public int score = 0;
void Awake()
{
if (Instance == null)
Instance = this;
else
Destroy(gameObject);
}
public void AddPoint()
{
score++;
Debug.Log("Score: " + score);
}
}
Attach this to a new empty GameObject named ScoreManager.
Once this is all set up, your checkpoint will randomly spawn, wait for the player, add a point, then move to a new random location — just like the delivery point system in Crazy Taxi or Simpsons: Road Rage.
Hope this helps get you moving forward! Let me know if you run into any snags or want help linking it to a UI score display.