- Home /
double collision detection in breakout game
Hi! im making an arkanoid clone, and im having some issues with the ball physics... since i noticed that unity's physics system doesnt work as i need, i decided to create my own. im using walls with tags and only trigger colliders to detect where the ball hits and change its direction... but on corners i found that when detects 2 colisions (becouse im using 2 colliders for side and top walls) it jus picks one and ignores the other one... when this happends the ball just go out of bounds becouse ignores one of the walls instead of go exacly on the opposite way as it should do...
Im using the OnTriggerEnter2D void to make the collisions but it just executes one time per frame.. its any way to execute it twice? or is there a collision list to know how many and wich objects im colliding with? thanks!!
Hi! Could you please show the code you're using for the ball's movement and collision?
Edit: I did a small test with these 2 classes and everything worked fine, even when both OnTriggerEnter2Ds happened in the same frame.
public class Wall : $$anonymous$$onoBehaviour
{
public Vector2 normal;
}
public class Ball : $$anonymous$$onoBehaviour
{
public Vector2 direction;
public float speed;
private int frame;
void Start()
{
frame = 0;
}
void Update()
{
frame++;
Vector2 pos = transform.position;
pos += direction.normalized * speed * Time.deltaTime;
transform.position = pos;
}
void OnTriggerEnter2D(Collider2D other)
{
Wall wall = other.GetComponent<Wall>();
if (wall == null) return;
Debug.Log("Frame: " + frame);
direction = Vector2.Reflect(direction, wall.normal);
}
}
Take a look: https://imgur.com/sa2oXLJ
Right, just as $$anonymous$$acDx said there should be no problem with the OnTriggerEnter methods. If you have issues it's more likely a problem with your collision response. Solving collisions can be much more difficult than just changing the direction of movement depending on the placement of your walls. At the time OnTriggerEnter is called your colliders overlap. If the new direction does not successfully seperate the colliders you won't get a new TriggerEnter until they were completely seperated.
Answer by Koyemsi · May 05, 2018 at 01:52 AM
Hi. To know what the ball has collided with, its script should have :
void OnTriggerEnter2D (Collider2D coll) { print ("Ball collided with " + coll.name); }
Your answer
Follow this Question
Related Questions
Performance of player detection: Colliders/Triggers vs OverlapCircle 2 Answers
problem solving ghost vertices with trigger and collision colliders 0 Answers
Physics2D.IgnoreCollision not working! 0 Answers
Ignore collision at the start of the game. 1 Answer
My OnCollisionEnter says i have 128 Collisions every collision 1 Answer