- Home /
How to resolve colliders being within each other without firing them off in all directions at maximum velocity
Hi all!
My game right now is restricted to the main camera, objects leaving that space are met with colliders (triggers) that move them to the otherside of the camera, respecting their position on the other side of the plane (like in pacman when you go off the one side).
Anyway, this functions fine, but occasionally when multiple objects exit the camera space and enter the collider together, they somehow end up within each other on the other side of the camera, which leads to a violent separation of the objects.
I'm hoping that someone might know of an easy way to resolve the intersecting colliders without launching them about, ideally without using continuous collision detection. Alternatively, there may be something wrong with the way I move the object to the opposite side of the screen, here's the script for that:
public bool HorizontalBorder;
void OnTriggerEnter2D(Collider2D other) {
Rigidbody2D Body = other.GetComponent<Rigidbody2D> ();
Vector2 Position = Body.position;
if (HorizontalBorder) {
if (Mathf.Sign (Body.velocity.x) == Mathf.Sign (Position.x))
Body.MovePosition (new Vector2 (-Position.x, Position.y));
} else {
if (Mathf.Sign (Body.velocity.y) == Mathf.Sign (Position.y))
Body.MovePosition (new Vector2 (Position.x, -Position.y));
}
}
Thanks for your help!
Your answer
Follow this Question
Related Questions
Make 2D projectile collide only from the outside 1 Answer
I can't manage to detect my collisions 0 Answers
2D collision detection in 3d games 0 Answers
Using OnTriggerEnter2D/OnTriggerExit2D with Time.timeScale set to 0 1 Answer
My OnCollisionEnter says i have 128 Collisions every collision 1 Answer