Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 givexa · Mar 26, 2021 at 06:38 PM · gameobjectunity 2doverlapspawner

How To prevent two GameObject spawners not to overlap.

So currently i am stuck on a problem, where i have two GameObject Spawners, one for Rock Spawner one For Diamond Spawner. The issue is the following: in some moments gameobject of rock and gameobject of diamond overlap with each other. I have Tried different ways, one was with ontriggerenter2d, however that was not a very good solution, is there any easy solution to prevent this problem for occuring?

Here are My four Scripts, i would like to know where can i write "The needed code" to prevent the problem of overlapping.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RockScript : MonoBehaviour
 {
     Rigidbody2D Myrigitbody;
     private Vector2 Bounds;
     void Start()
     {
         Myrigitbody = GetComponent<Rigidbody2D>();
         Bounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
         
     }
 
     // Update is called once per frame
     void Update()
     {
         MoveRock();
         RemoveRock();
     }
     public void RemoveRock() 
     {
         
         if (-1 * transform.position.y > Bounds.y)
         {
             Destroy(gameObject);
         }
 
         
     }
 
     public void MoveRock() 
     {
         Myrigitbody.velocity = new Vector2(0f, -1f);
     }
 
     
 }

Heres the Rock Spawner Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RockSpawner : MonoBehaviour
 {
 
     public GameObject Rock;
     private Vector2 RockBound;
     
 
     public void Start()
     {
         RockBound = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
         StartCoroutine(Mycourotine());
     }
 
     private void SpawnRock() 
     {
         GameObject rock = Instantiate(Rock);
         rock.transform.position = new Vector2(Random.Range(-RockBound.x, RockBound.x), 2*RockBound.y);
 
         
     }
 
 
 
     IEnumerator Mycourotine() 
     {
         while (true) 
         {
             yield return new WaitForSeconds(3f);
 
             
              SpawnRock();
             
             
             
         }
     }
 }
 

The Diamond Script is Exactly The Same, so i would likely add more gameobject spawners, which means that i have to write overlapping code in each of them right? So in a word i would really appreciate if someone could tell me how can i prevent this problem in my code.

Comment
Add comment · Show 4
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 HellsHand · Mar 26, 2021 at 08:08 PM 0
Share

I would check the output of the Random.Range statements, it might be possible that they produce the same results. If so it may be as simple as changing the seed for each spawners random.

avatar image Vivien_Lynn · Mar 26, 2021 at 08:15 PM 1
Share

There is a function to get the distance of two positions Vector3.Distance() (https://docs.unity3d.com/ScriptReference/Vector3.Distance.html). If they are too close, try to generate a new target position.

avatar image HellsHand Vivien_Lynn · Mar 26, 2021 at 08:20 PM 0
Share

This is a better method actually, just cause the seeds are different wouldn't guarantee that they will never randomly come up with the same value.

avatar image logicandchaos Vivien_Lynn · Mar 27, 2021 at 01:34 PM 0
Share

and then you need to put it in a while loop, like while(distance

0 Replies

· Add your reply
  • Sort: 

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

178 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

instantiate multiple gameobject of the same gameobject 2d 1 Answer

Game Object Not Working Properly 0 Answers

why gameobject / raycast when dragged is not accurately following the touch position in unity 0 Answers

is it Possible to use navigation on non UI elements? 1 Answer

Don't Destroy on Load not working for me 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