Now the problem I’m facing is that some of the objects are spawned at the same position. I would like to the object to re-position itself in that case. How can I detect whether there is an object in the same spot as the newly spawned object?
I tried solving it with function OnTriggerStay (other : Collider) but since the object is directly spawned in the same spot it doesn’t seem to work.
Do you have any hint where the problem lies and maybe can give me a hint on how to solve the problem?
Best practice is u make every instantiated object as a child of one empty parent object. Now before going to instantiate a new object get all child objects of that empty parent object and compare their transform.position is match with ur new random.range value. Thats why u can check if a object already there or not.
The other suggestions might work in some circumstances, but none of them take into account the size of the objects. Therefore you might still get objects that overlap with each other (unless they’re smaller than one unit for this scenario).
With the code that Ram posted, it might not work because comparing Vector3 isn’t very reliable. Vector3 is a class comprised of float values, and a quick search on Google will tell you that directly comparing floats is an accident waiting to happen. It would be better to work out the difference between the two positions and check if it’s under a certain small threshold. His code also doesn’t factor in that the local position of an object might be different from it’s world position.
#pragma strict
var objectCount : int = 20;
var myObject : GameObject;
var minRange : int = -5;
var maxRange : int = 5;
var randomPos :Vector3;
var objectPosition = new Array();
function Start ()
{
// create an Object for each level
for (var a = 0; a < globalVars.levelCount; a++)
{
// create a randomposition
randomPos = Vector3(Random.Range(minRange,maxRange),1,Random.Range(minRange,maxRange));
//start searchig for values in the array
for ( var value in objectPosition)
{
// if the value is not inside the array
if ( value != randomPos)
{
// spawn an object
Instantiate(myObject,randomPos, transform.rotation);
objectCount -= 1;
objectPosition.Add(randomPos);
}
}
}
}
function Update ()
{
}
BUT it doesn’t work. When compiling the script Unity isnot prompting any errors. Any suggetsion where I went wrong?
var dist = Vector3.Distance(GameObject.Find("object").transform.position, transform.position);
//Then check how far they are away.
if(dist < .2)//Checks if there within .2 of each other.
{
//do something
}
Why use ‘Any(p=> p == randomPos)’ when one can use ‘Contains(randomPos)’
@OP using the ‘p’ in Any will fail because US doesn’t allow that lambda syntax IIRC - you’d need a more verbose form. For either method to work you’ll need to import System.Linq IIRC. You may have to change objectPosition to a List for this to work - in which case you’ll need to import System.Collections.Generic IIRC.
I couldn’t remember whether that skanky Array class has a Contains method or not so I went with the safer option… If its got one then sure, go for it. You’d be better off using HashSet though.
So I had some help from a friend. Here is the solution to the problem:
var myObject : GameObject;
var minRange : int = -3; // min spawn range
var maxRange : int = 3; // maxspawn range
var randomPos :Vector3; // random position genearted
var checkposition:Vector3; // used to check wether the position is already in the array
var place:boolean = true; // can the object be placed?
var objectPosition = new Array(); // array that contains used up positions
function Start ()
{
// create an Object for each level
for (var a = 0; a < globalVars.levelCount; a++)
{
// create a randomposition
randomPos = Vector3(Random.Range(minRange,maxRange),1,Random.Range(minRange,maxRange));
//start searchig for values in the array
place = true;
for ( checkposition in objectPosition)
{
//if the value is not inside the array
if ( checkposition == randomPos)
{
place = false;
}
}
if (place == true)
{
//spawn an object
Instantiate(myObject,randomPos, transform.rotation);
objectPosition.Add(randomPos);
}
}
}