Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by toon1334 · Apr 21, 2015 at 07:40 PM · instantiate3drandomspawnoverlap

Instantiate Random 3D Objects/platforms that dont overlap

I am trying to instantiate 3 different prefab object platforms at random. These platforms have box colliders and rigid bodies ( is kinematic) attached to them. When they are instantiated their x and y positions are also random. These plat forms also move up/down or left/right. I am trying to figure out how to get them so they don't instantiate on top of each other and so when they move, don't collide/pass through the other platforms. Rigid body(with gravity) players land on the platforms,so I dont want them to fall through the platforms.

I am thinking that one would need to check the previously made platform x and then if the new randomly generated x is the same, randomly generate a new x, or something like this, though I am not sure how to do this with my code.

I am new to programming so please be specific in your answers (ie. If I need to add a line of code or take out a part please tell me exactly what to do) Any help would be greatly appreciated.

 public class cEndless : MonoBehaviour {
 
     public Transform player;
     public float playerHeightY;
     
     public Transform PlatAA;
     public Transform PlatAB;
     public Transform PlatAC;
     
     private int platNum; 
 
     public int currentNumPlat; //Self-explained
     public int maxNumPlat; //Self-explained
     
     // Use this for initialization
     void Start () {
         player = GameObject.FindGameObjectWithTag("Player").transform;    
         currentNumPlat = 0;
         maxNumPlat = 3;
 
         if (currentNumPlat == 0)
         {            
             Vector2 posAB = new Vector2(0,0);             
             Instantiate (PlatAA, posAB, Quaternion.identity);
             currentNumPlat += 1;
         }
     }    
     // Update is called once per frame
     void Update () {
         
         playerHeightY = player.position.y;
         
         if (currentNumPlat < maxNumPlat)
         {
             PlatformSpawner();            
         }    
     }
         
     void PlatformSpawner()
     {
         if (currentNumPlat < maxNumPlat) 
         {
             platNum = Random.Range(1,4);
             float x = Random.Range (-8, 8);
             float y = Random.Range (playerHeightY + 1.3f, playerHeightY +3.3f);
             Vector2 posXY = new Vector2 (x, y);
         
             
             if (platNum == 1) 
             {
                 Instantiate (PlatAA, posXY, Quaternion.identity);
             }
             if (platNum == 2) 
             {
                 Instantiate (PlatAB, posXY, Quaternion.identity);
             }
             if (platNum == 3) 
             {
                 Instantiate (PlatAC, posXY, Quaternion.identity);
             }
 
             currentNumPlat += 1;
         }
     }
 }
 
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 AlwaysSunny · Apr 21, 2015 at 07:40 PM 0
Share

This requirement will take some planning. Your placement method must consider the placement and movement extents of all previous placements. This means, for every platform that can move, you must know in advance its possible range of motion.

There are several valid ways to handle this, but perhaps the most intuitive involves adding a large, temporary collider to your "moving platform" prefabs.

This collider should fully encompass the platform's possible range of motion. When placing new random platform in a random position, you'll respond to any OnCollisionEnter events between these helper "boundary" colliders by destroying the newer platform and reporting to the generator that the last placement was unsuccessful.

This approach implies that generation will take place over several frames in a coroutine, but it is probably the easiest way to satisfy this requirement.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hbalint1 · Apr 21, 2015 at 08:59 PM

I would suggest this: http://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html You generate random coordinates and with the OverlapSpehre you check is something in a given radius. If yes, you can generate new coordiantes.

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 AlwaysSunny · Apr 21, 2015 at 09:35 PM 0
Share

This approach achieves separation, but not cohesion, which I assumed was an implied requirement.

If they wanted separation without cohesion, they could simply ensure no two platforms are within X distance of each other, where X is twice the greatest distance a platform can travel from its origin, and be done. In that case, it would be wasteful to use physics.OverlapSphere.

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

Why do objects keep spawning on top of one another? 0 Answers

avoid overlap problem when randomly instantiating 0 Answers

Object Spawning Randomly 0 Answers

Spawn gameobjects without overlap 0 Answers

How can i randomly create an array of objects without colliding one another? 1 Answer


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