- Home /
overlapping of gameobjects position
hi there, i have script that has 2 public array variables called spawnPoints and spawnObjects, i have 9 spawnPoints and 4 spawnObjects. These 4 spawnObjects are placed on 4 spawnPoints. Here what i am doing is whenever i press the mouse button i m changing the position of spawnObjects to other spawnPoints, its working fine but the spawnObjects are overlapping some times.
using UnityEngine;
using System.Collections;
public class Forward : MonoBehaviour
{
public GameObject[] spawnPoint;
private GameObject spawn;
public GameObject[] spawnObjects;
private GameObject objectSpawn;
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
int a = Random.Range (0, spawnPoint.Length);
int b = Random.Range (0, spawnObjects.Length);
spawn = spawnPoint [a];
objectSpawn = spawnObjects [b];
objectSpawn.transform.position = spawn.transform.position;
}
}
}
So, you state that you have overlapping spawn points...
... But you don't mention the issue you are having with this.
I'm assu$$anonymous$$g that the problem is you don't want you spawn objects to overlap?
Let us know.
Create a copy of your spawnPoints. Whenever you picked a position for a spawnObject remove the chosen position from the copy. This is easily done by having a list ins$$anonymous$$d of an array. Loop through the array to adding each entry to that list to fill it. Call RemoveAt to remove the current index you found.
Check out the gridPositions list in this tutorial for reference
@$$anonymous$$Buckner: ya exactly i want to do that
Or the better way is to create an array which contains the spawn position and make a list that contains numbers(unique random numbers) nothing but index of the spawn postion array.
Untested code but should work. Thank you Nsk
//$$anonymous$$ethod required to create random numbers
IEnumerable<int> UniqueRandom(int $$anonymous$$Inclusive, int maxInclusive)
{
List<int> candidates = new List<int>();
for (int i = $$anonymous$$Inclusive; i <= maxInclusive; i++)
{
candidates.Add(i);
}
System.Random rnd = new System.Random();
while (candidates.Count > 0)
{
int index = rnd.Next(candidates.Count);
yield return candidates[index];
candidates.RemoveAt(index);
}
}
void PositionSpawnObjects()//method to position the spawn objects randomly WRT to Desired positions already stored in spawnPositions array
{
for(int i=0;i<SpawnPositionArr.Length;i++)
{
spawnObjectsArr[i].transform.position = spawnPositions[RandomNumbersList[i]];//spawnPositions-array consits of desired position for da objects to spawn
}
}
void Update () {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
{
foreach (int i in UniqueRandom(0,4)) //gives random numbers from 0 to 4 and adds those numbers to an list
{
print(i);
RandomNumbersList.add(i);//this list contains numbers without duplicated
}
PositionSpawnObjects();
}
}
Your answer
Follow this Question
Related Questions
How to increment value of axis when something happens 2 Answers
Best way to spawn new objects that arent already in the scene? 1 Answer
NullReferenceException: Instatiate with construtor OR in Inspector 1 Answer
AR Object stuck in front of camera 0 Answers
Why does this script spawn 13 ClickEffect prefabs instead of 1? 2 Answers