Re spawn Point Script

Hello Unity community, I was wondering how difficult it would be to make a spawn script. This spawn script though would be special :slight_smile: The main master plan behind it is I would like it to have the player spawn to the nearest spawn point. Thank you for any scripts or resources. I have looked at tutorials but they did not have what I am looking for. "sorry if the paragraph slash question sounds bad, so tired cant think lol.

First, keep all of your spawn-points in an array-

public Transform[] spawnPoints;

Then, when you need to respawn at one of them, use this:

public Vector3 RespawnAtNearestPoint(Vector3 curPos)
{
    Transform closestPoint = spawnPoints[0];
    float shortestDistance = Vector3.Distance(curPos, closestPoint.position);
    foreach(Transform trans in spawnPoints)
    {
        float curDist = Vector3.Distance(curPos, trans.position);
        if(shortestDistance > curDist)
        {
            shortestDistance = curDist;
            closestPoint = trans;
        }
    }
    return closestPoint.position;
}

I took the code Kieren offered so graciously and adapted it to allow the respawning GameObject to use the spawn points rotation as well. There may have been a better way to do this, but I figured someone else might be able to use it as well. Simply add as many empty GameObjects to the list as you’d like and position/rotate them to your liking. Your object will both spawn there and be rotated to face the Z-axis of the spawn.

I took the liberty to remove all velocity and force from the rigidbody I’m ‘respawning’ in the OnCollisionEnter function. Because it is not being created and just moved, it holds all of the original forces applied to it.

public GameObject[] spawnPoints;

        void OnCollisionEnter(Collision collision) {
            if (collision.collider.tag == "Respawn"){
                ArrayList respawnList = RespawnAtNearestPoint(transform.position);
                Vector3 respawnSpot = (Vector3) respawnList[0];
                transform.position = respawnSpot;
                Quaternion respawnRotation = (Quaternion) respawnList[1];
                transform.rotation = respawnRotation;
                rigidbody.velocity = Vector3.zero;
                rigidbody.angularVelocity = Vector3.zero;
            }
        }

        ArrayList RespawnAtNearestPoint(Vector3 curPos){
            ArrayList respawnList = new ArrayList();
            Transform closestPoint = spawnPoints[0].transform;
            float shortestDistance = Vector3.Distance(curPos, closestPoint.position);
            foreach(GameObject point in spawnPoints){
                float curDist = Vector3.Distance(curPos, point.transform.position);
                if(shortestDistance > curDist){
                    shortestDistance = curDist;
                    closestPoint = point.transform;
                }
            }
            respawnList.Add(closestPoint.position);
            respawnList.Add(closestPoint.rotation);
            return respawnList;
        }