OntTriggerEnter2D keeps detecting other ones
Hello, I'm trying to make a little game, in which you should be able to roam in space for ever. 1 background sprite has a trigger collider which instantiates 8 background sprites in each direction. The problem is that one background tile keeps detecting an other on the same spot. I tried fixing this with a Physics2D.OverlapBox, but this doesnt work. This is my code; using System.Collections; using System.Collections.Generic; using UnityEngine;
public class background : MonoBehaviour
{
public GameObject bg;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (Physics2D.OverlapBox(new Vector3(transform.position.x - 30, transform.position.y - 30, 0),new Vector2(30,30),0)) {
// 0 x 0
}
else
{ Instantiate(bg, new Vector3(transform.position.x - 30, transform.position.y - 30, 0), Quaternion.identity);
}
if (Physics2D.OverlapBox(new Vector3(transform.position.x, transform.position.y - 30, 0), new Vector2(30, 30),0))
{
// 0 X 0
}
else
{Instantiate(bg, new Vector3(transform.position.x, transform.position.y - 30, 0), Quaternion.identity);
}
if (Physics2D.OverlapBox(new Vector3(transform.position.x + 30, transform.position.y - 30, 0), new Vector2(30, 30),0))
{
}
else
{
Instantiate(bg, new Vector3(transform.position.x + 30, transform.position.y - 30, 0), Quaternion.identity);
}// 0 0 0
if (Physics2D.OverlapBox(new Vector3(transform.position.x - 30, transform.position.y, 0), new Vector2(30, 30),0))
{
}
else
{
Instantiate(bg, new Vector3(transform.position.x - 30, transform.position.y, 0), Quaternion.identity);
}
if (Physics2D.OverlapBox(new Vector3(transform.position.x - 30, transform.position.y + 30, 0), new Vector2(30, 30),0))
{
}
else
{Instantiate(bg, new Vector3(transform.position.x - 30, transform.position.y + 30, 0), Quaternion.identity);
}
if (Physics2D.OverlapBox(new Vector3(transform.position.x + 30, transform.position.y, 0), new Vector2(30, 30),0))
{
}
else
{
Instantiate(bg, new Vector3(transform.position.x + 30, transform.position.y, 0), Quaternion.identity);
}
if (Physics2D.OverlapBox(new Vector3(transform.position.x + 30, transform.position.y + 30, 0), new Vector2(30, 30),0))
{
}
else
{ Instantiate(bg, new Vector3(transform.position.x + 30, transform.position.y + 30, 0), Quaternion.identity);
}
if (Physics2D.OverlapBox(new Vector3(transform.position.x, transform.position.y + 30, 0), new Vector2(30, 30),0))
{
}
else
{
Instantiate(bg, new Vector3(transform.position.x, transform.position.y + 30, 0), Quaternion.identity);
}
}
}
Someone please help me!
Your answer
Follow this Question
Related Questions
need help on character movement 2 Answers
How can I make a script that plays an animation then requires you to press a key 0 Answers
Mouse Look clamping problem 0 Answers
"Creating a Basic Platformer Game" help 0 Answers
Scaling a text to fill 50% of screen width and 50% of height, no matter whats inside C# 0 Answers