Answer the question
In order to leave comments, you need to log in
Protection against the intersection of generated objects - Unity 2d?
Hello, dear programmers) I can’t solve the problem, please help)
It is necessary to make sure that the created objects do not intersect (that is, do not overlap each other).
The code:
public GameObject player;
public GameObject platformPrefab;
public float minY = -4.5f;
public float maxY = 4.7f;
Vector2 spawnFix;
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Platform"))
{
Vector2 targetPosition = new Vector2(Random.Range(-1.8f, 1.8f), player.transform.position.y + Random.Range(4.5f, 4.7f));
GameObject p = Instantiate(platformPrefab, targetPosition, Quaternion.identity);
Destroy(collision.gameObject);
}
}
Answer the question
In order to leave comments, you need to log in
Well, check after instancing - if it crossed - move / delete. What exactly is not working for you?
PS yes, my algorithm itself is shit, but you need to start small
public GameObject player;
public GameObject platformPrefab;
public float minY = -4.5f;
public float maxY = 4.7f;
public float ratio = 1.2f;
public float sizeplatformony;
public float lastvalue;
Vector2 spawnFix;
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Platform"))
{
Value = player.transform.position.y + Random.Range(MinY MaxY);
Vector2 targetPosition = new Vector2(Random.Range(-1.8f, 1.8f), value);
if(Value-lastvalue > sizeplatformony + ratio){
GameObject p = Instantiate(platformPrefab, targetPosition, Quaternion.identity);
Destroy(collision.gameObject);
lastvalue = Value;
} else {
GameObject p = Instantiate(platformPrefab, targetPosition+ratio, Quaternion.identity);
Destroy(collision.gameObject);
lastvalue = Value + ratio;
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question