[C#] How do I randomly spawn objects inside of a radius without them overlapping.
I am really a noob at this but I am trying to make it so that multiple instances of a Game Object spawn in a random position within a set radius. All I am able to do so far is to make it spawn in variations of a straight line
using UnityEngine;
using System.Collections;
public class WallGen : MonoBehaviour
{
public GameObject thePlatform;
public Transform GenerationPoint;
public float distancebetween;
private float platformWidth;
// Use this for initialization
void Start()
{
platformWidth = thePlatform.GetComponent<CircleCollider2D>().radius;
}
// Update is called once per frame
void Update()
{
if (transform.position.y < GenerationPoint.position.y)
{
transform.position = new Vector3(transform.position.x + platformWidth + distancebetween, transform.position.y, transform.position.z);
Instantiate(thePlatform, transform.position, transform.rotation);
}
}
}
never$$anonymous$$d I figured out my question however I now have another one
using UnityEngine;
using System.Collections;
public class WallGen : $$anonymous$$onoBehaviour
{
public GameObject thePlatform;//Object being spawned.
public Transform GenerationPoint;//Not really immportant kinda left over from editing a bunch
public float distancebetween;//Not important
public int Enemy;
private float platformWidth;//Null
public int SpawnLimit;//Limit of instances of an object
public int SpawnCount;//Should be the amount of instance of the object
public bool spawn;//To spawn or not to spawn is the question
public float waitTime;//Explanatory
public float timer = 0.0f;//Explanatory
// Use this for initialization
void Start()
{
platformWidth = thePlatform.GetComponent<CircleCollider2D>().radius;
}
// Update is called once per frame
void Update()
{
if (spawn)
{
timer += Time.deltaTime;
if (timer > waitTime)
{
if (SpawnCount < SpawnLimit)
{
Spawn();
timer = 0.0f;
}
else
{
spawn = false;
Debug.Log("Spawn Limit has been reached");
}
}
}
}
void OnCollisionEnter2D(Collision2D other)//This is to stop collisions/Overlapping by deleting an objects that collide with each other.
{
if (other.gameObject.tag == "Obstacle")
{
Destroy(this.gameObject);
}
}
public void Spawn() {
transform.position = new Vector2(Random.insideUnitCircle.x * 10, Random.insideUnitCircle.y * 10);//Spawning objects randomly within a circle
Instantiate(thePlatform, transform.position, transform.rotation);
SpawnCount++;
}
}
So using this code how would I get the Random.insideUnitCircle to follow the gameObject its attached to cause right now it just stays in its original position
Answer by NoseKills · Sep 22, 2016 at 06:12 AM
how would I get the Random.insideUnitCircle to follow the gameObject its attached to
How do you make a random range be in relation to a target number? You add the random number to the target number.
var random = Random.Range(-2f, 2f);
var target = 5;
var result = target + random; // 2 smaller or greater than 5
It's exactly the same with vectors. You should just create a random vector and add that to the position of the target GameObject.
public void Spawn() {
var randomPos = (Vector3)Random.insideUnitCircle * 10;
randomPos += transform.position;
Instantiate(thePlatform, randomPos, transform.rotation);
SpawnCount++;
}
Thank you for this but I now have another question as I am progressing along I would like it to deleted everything that passes outside of the radius of the circle so I don't waste a bunch of memory space