To find the nearest spawn point, follow these steps:
- Add the following method to the C# script class called SpawnPointManager:
public GameObject GetNearestSpawnpoint (Vector3 source){
GameObject nearestSpawnPoint = spawnPoints[0];
Vector3 spawnPointPos = spawnPoints[0].transform.position;
float shortestDistance = Vector3.Distance(source, spawnPointPos);
for (int i = 1; i < spawnPoints.Length; i++){
spawnPointPos = spawnPoints[i].transform.position;
float newDist = Vector3.Distance(source, spawnPointPos);
if (newDist < shortestDistance){
shortestDistance = newDist;
nearestSpawnPoint = spawnPoints[i];
}
}
return nearestSpawnPoint;
}
- We now need to change the first line in the C# SpawnBall class so that the spawnPoint variable is set by a call to our new method; that is, NearestSpawnpoint(...):
private void CreateSphere(){
GameObject spawnPoint...