Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by hellojbb1234 · Sep 22, 2016 at 05:02 AM · 2d game

[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);
       
                     
                 }
 
 
     }
 }
 

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hellojbb1234 · Sep 21, 2016 at 11:08 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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++;
  }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hellojbb1234 · Sep 22, 2016 at 02:02 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Camera Movement Panning with middle mouse 1 Answer

Problem with tracking multiple object with multitouch 0 Answers

Problems with player jumping (Welcome to Unity Answers The best place to ask and answer questions ) 0 Answers

please help. I want to change the screen size of my 2d unity game,How do I change the screen size of my 2D game? Yes, I'm a beginner and I'd appreciate if someone can answer my question T_T 1 Answer

Saving Instantiated Objects 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges