- Home /
Question by
BigKahuna311 · Mar 27, 2020 at 10:34 PM ·
2dcollision detectioncollision2d
Avoid Initial Collisions,Avoiding Initial Collisions
Hey, so I'm attempting to ignore all collisions between objects with the tag 'enemy' and I'm currently using the method below..
private void OnCollisionEnter2D(Collision2D Collision)
{
if (Collision.gameObject.CompareTag("Enemy"))
{
Physics2D.IgnoreCollision((rb2d.GetComponent<CircleCollider2D>()), Collision.gameObject.GetComponent<CircleCollider2D>());
}
}
This only seems to work for any collisions AFTER the initial collision between any two objects. They only IgnoreCollision after they have already collided once initially, afterward they float right through each other. How can I fix it so they never hit each other in the first place?
Thanks for any help
Comment
Best Answer
Answer by Magso · Mar 27, 2020 at 10:54 PM
This is because it isn't executed until OnCollisionEnter2D()
is called which is quite self explanatory. Either put it in Awake()
, Start()
or put a child trigger around it and use OnTriggerEnter2D()
.
private void OnTiggerEnter2D(Collider2D collider){
{
if (collider.transform.parent.gameObject.CompareTag("Enemy"))
{
Physics2D.IgnoreCollision((rb2d.GetComponent<CircleCollider2D>()), collider.transform.parent.gameObject.GetComponent<CircleCollider2D>());
}
}