There’s probably a really simple way to do this that’s right under my nose, so I apologize if I’m wasting everyone’s time. My game has a game object that spawns randomly within a certain range, using Random.Range (min, max). I have it so they spawn randomly, but could there be an easier way to spawn things randomly? Since I have multiple of this randomly spawning game object, in order to set up the Random.Range for all of them, I’d have to right a different script for every one of these game objects, which would get really tedious, so I was just wondering if there is some other way to do what I’m trying to do. I’ve tried this code:
function Start ()
{
// Spawns randomly at the start of the level.
var newPositionStartX : float = Random.Range (-5.5, 5.5);
transform.position.x = newPositionStartX;
var newPositionStartY : float = Random.Range (-5.5, 5.5);
transform.position.y = newPositionStartY;
}
function OnCollisionEnter2D (col : Collision2D)
{
if (col.gameObject.CompareTag ("Obstacle")
{
// If colliding with game objects tagged "Obstacle," respawn randomly
// (basicly, repeat the Start function).
var newPositionColX : float = Random.Range (-5.5, 5.5);
transform.position.x = newPositionColX;
var newPositionColY : float = Random.Range (-5.5, 5.5);
transform.position.y = newPositionColY;
}
}
But it didn’t work. In fact, the game objects this code were attached to skipped the whole OnCollisionEnter2D function entirely. I don’t know why it skips this code, but I think everything is formatted correctly. Am I doing something wrong, am I stupid or is there another way to do this? Any help will be appreciated!
By the way, I’m using the free version of Unity 4.3.4f1 on Windows 8.1 and coding with UnityScript, if that makes any difference. For answers, I will except either UnityScript or C#, preferably UnityScript.
What I tell that guy is to call a shpere cast in the spot he is going to put the object. If it hits anything, or anything tagged with a certain tag then have your code pick a new spot. Else if you don’t hit anything then spawn your object.
I tried to read that, but I’m new to scripting and Unity3D, so most of what you said, especially involving the SphereCast stuff is foreign to me. I read about SphereCast in the script reference and I don’t understand it. Could you give an example of the code you are talking about because that would really clear things up for me! Thanks!
By the way, I find it funny that I asked this question just a few days after someone else asked the same question!
Yea I can do that.
Ok I made the code but one part isn’t working. For some reason i can’t find the collider it hits. Maybe someone can figure out what I did wrong. I commented the code well for you though.
#pragma strict
var numToSpawn : int = 10;
var minX : float = -5;
var maxX : float = 5;
var minY : float = -5;
var maxY : float = 5;
var minZ : float = 0;
var maxZ : float = 0;
var objToSpawn : GameObject;// The prefab you want to spawn
function Start ()
{
PlaceRandomObj(objToSpawn, numToSpawn);// Call the function passing the object to spawn and how many it wants
}
function PlaceRandomObj(prefab : GameObject, numToSpaw : int)
{
var count : int = 0;// counts so it knows when to break it
var infiLoopBreak : int = 0; // Keeps you from infint looping and crashing unity
var hit : RaycastHit; // Where your hit info is stored
while(count < numToSpawn && infiLoopBreak < 200)
{
var newPos : Vector3 = Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), Random.Range(minZ, maxZ)); // Randomized position
Physics.SphereCast(newPos, 10.0, Vector3(0,0,0), hit, Mathf.Infinity);// Checks a radius and sees if there are any objects in it.
//If your objects are to far spread out or not enough are spawning play with the num 10;
//THIS IS NOT WORKING. ALWAYS COMES UP NULL DON'T KNOW WHYA
if(hit.collider != null)
{
print(hit.collider.name);
infiLoopBreak++; // IF null happens to many times it will stop looping
}
else
{
Instantiate(prefab, newPos, prefab.transform.rotation);// if no object was hit it spawn the object
count++;// add to the count so it knows when to stop
}
infiLoopBreak++;// Kinda a catch all. I don't believe this would ever be called but I like it there just in case.
}
}