After watching a rewarded video, I want to reward a game player with an extra life in gameplay.
So for this, I have again respawned player car again within the track.
But sometimes this kind of situation get occurred:
Player Car gets overlapped with other AI cars.
Total 9 to 13 cars were moving on the same track, so what strategy I require to apply in respawning?
So that player car doesn’t overlap other moving cars.
Okay, I managed to solve the problem:
Performed these steps:
- Created multiple respawn positions on the track
- Then at respawn time, checked all cars position respective to currently selected spawn position
- At last, whichever position get selected that is safe for respawning
Here is the code that I used for this purpose:
private Vector3 FindSafePlaceForSpawning()
{
Vector3 spawnPosition = playerRespawnOptions[0].position;
for (int i = 0; i < playerRespawnOptions.Length; i++)
{
bool isAnyNearCar = false;
for (int j = 0; j < gamePlayCarList.Count; j++)
{
if (Vector3.Distance(playerRespawnOptions*.position, gamePlayCarList[j].position) < 2f)*
{
isAnyNearCar = true;
break;
}
}
if (!isAnyNearCar)
spawnPosition = playerRespawnOptions*.position;*
}
return spawnPosition;
}