- Home /
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.
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.
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.
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.
and then you need to put it in a while loop, like while(distance
Your answer
Follow this Question
Related Questions
instantiate multiple gameobject of the same gameobject 2d 1 Answer
How can i solve this issue?? i need real help. 1 Answer
Function to assign gameobject components with parameter strings... 1 Answer
Why I get Unity Ads Missing Reference error? 1 Answer
How to convert GameObject array to Transform array in C#? 2 Answers