- Home /
Why my player collides with the bullet the first time?
public class BulletScript : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Player")
{
Physics2D.IgnoreCollision(other.collider, GetComponent<Collider2D>());
}
else if (other.gameObject.tag == "Enemy")
{
Destroy(other.gameObject);
Destroy(gameObject);
}
else Destroy(gameObject);
}
}
This up here is my bullet script. Why when I shoot I can jump on or collide with the bullet for the first time? After that I can go trough it. Thanks in advance!
Answer by paynob · Feb 25 at 08:40 PM
Set your bullet collider3D as isTrigger and use OnTriggerEnter2D instead of OnCollisionEnter2D. That way, you can delete your
if (other.gameObject.tag == "Player")
{
Physics2D.IgnoreCollision(other.collider, GetComponent<Collider2D>());
}
Or even better use LayerCollisionMasks
if I do on trigger enter It doesn't let me put other.collider or other.gameObject.collider
Oh, nevermind. I just put other.gameObject.GetComponent()
Your answer
Follow this Question
Related Questions
Object with rigidbody bounces after hit. 0 Answers
How to get GameObject with its tag when ignoring Collision? 2 Answers
2D collider problems 1 Answer
How to continue the work in onTriggerStay, when it disables, for remaining objects? 2 Answers
How can I detect a collision between two clones and delete them both? 1 Answer