Hi there, In my 2D game, game objects regularly spawn around the player, they can spawn anywhere on the screen as long as they stay within the screen as the player does not move. However the problem I have is that sometimes objects spawn directly under the player and cannot be seen.
How can I make sure the objects spawn a certain distance away from the player but still within the screen.
This is what I am using at the moment, any help would be great?
GameObject point = GameObject.Instantiate (objectToSpawn, new Vector2 (Random.Range (-3, 3), Random.Range (-4, 6)), Quaternion.identity) as GameObject;
Generate the random location first, not in the method call. Then you can do something like
if((newPos-playerPos).magnitude < minDistance)
newPos = [regenerate here];
Then just pass the newPos to the instantiate function once you know you have a good one.
Untested. Note that Random.Range(int,int) returns a value that doesn’t include the second value because integers are used. If you want a much “smoother” range change them for floats.
int endlessLoopCount = 0;
bool error = false;
Vector2 point = GetRandomPoint();
/*
this will keep getting a new point if the generated point is within minimumDistanceFromPlayer
it also prevents locking up the game incase the point is within minimumDistanceFromPlayer 30 times in a row, or if something goes wrong it wont lock up unity and require a restart.
*/
while(Vector2.Distance(point, playerPosition) < minimumDistanceFromPlayer || endlessLoopCount > 30)
{
point = GetRandomPoint();
endlessLoopCount++;
if(endlessLoopCount > 30)
{
Debug.Log("Something made GetRandomPoint() return within minimumDistance too many times, exiting.");
error = true;
}
}
if(!error)
{
GameObject = (GameObject)Instantiate(objectToSpawn, point, Quaternion.identity);
}
//this will get a point from (-3 to 2, -4 to 5)
Vector2 GetRandomPoint()
{
return new Vector2(Random.[Range][1](-3,3), Random.Range(-4,6));
}
Hi, thanks for both suggestions! Although fortunately I managed to get this working last night using the code below. It just does a simple check distance and then restarts the function and breaks if it needs to. However as Xortrox pointed out, there is always the Small chance that the function could loop forever, can you spot any easy way to counter this as you have done in yours?
void GreenLightsOn () {
for (var i = 0; i < Random.Range(1,GreensSpawnAmount); i++) {
GameObject spawnedLight = GameObject.Instantiate (light, new Vector2 (Random.Range (-3, 3), Random.Range (-4, 6)), Quaternion.identity) as GameObject;
float dist = Vector2.Distance (spawnedLight.transform.position,transform.position);
if(dist <= 0.7f) {
Destroy (spawnedLight);
GreenLightsOn();
Debug.Log ("TOO CLOSE");
break;
}
clickScreen.GetComponent<ClickScreen> ().Spawned ++;
}
As long as you know the max radius you want I think this might be your friend: Unity - Scripting API: Random.insideUnitCircle
Then if you want a minimum distance I would do something like this:
while(Vector2.distance(player.transform.position, instantiatedStuff.transform.position) < minDistance)
{
instantiatedStuff.transform.position += (instatiatedStuff.transform.position - player.transform.position).normalized;
}