Physics2D.IgnoreCollision() Not working the first time it collides.
I have a code on a projectile that is supposed to go through the gameObject tagged as "Hexagono" if it's transparency is lower that a certain value, so I'm using Physics2D.IgnoreCollision() but for some reason it never works the first time the projectile collides (It immediately bounces off). However because my projectile bounces, it does seem to work every other time it collides with the same object (It successfully goes through the object).
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag.Equals("Hexagono"))
{
if (collision.gameObject.GetComponent<SpriteRenderer>().material.color.a <= 0.4f)
{
Physics2D.IgnoreCollision(collision.gameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>());
}
}else{
Vector2 tweak = new Vector2(Random.Range(-1f, 1f), 0f);
GetComponent<Rigidbody2D>().velocity += tweak;
}
}
Your answer
Follow this Question
Related Questions
Start collision detection of particles after certain time 0 Answers
How to create a fixed joint when two objects collide? 0 Answers
Trying to get collider object name,Trying to get colliding object name 0 Answers
Rotating a character with character controller when colliding a wall. 1 Answer
RigidBody Collision Detection Error 1 Answer