- Home /
Bullet won't collide with enemy
I have two objects; an enemy object and a bullet object. They both have 2Dboxcolliders and 2Drigidbodys and the bullet object has 'Is Trigger' set to true.
In the enemy script I have the following based off of a tutorial:
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Bullet"))
{
sr.material = matwhite;
Debug.Log("got shot");
Destroy(collision.gameObject);
health--;
if (health <= 0)
{
KillSelf();
}
}
}
When making contact with the enemy however, the bullet will just go right through and not collide at all,
I have checked that the bullet does have the bullet tag, I'm not sure what is causing the problem since it seems to work well enough in the tutorial i am following and i am pretty sure i have everything else the tutorial needs. However if there is another fairly straightforward way to fix my problem apart from this am open to suggestions.
Answer by Razputin · Jul 29, 2021 at 12:44 PM
"the bullet object has 'Is Trigger' set to true"
If you have OnTriggerEnter on the Enemy then the enemy is the trigger, not the bullet. You'd need the enemy IsTrigger to be true, and the bullet's IsTrigger to be false.
It almost seems like you think OnTriggerEnter means "When a trigger enters me". But that's not the case it means "I am a trigger, and when something enters me I do something."
Me personally I would change this to put the OnTriggerEnter on the bullet, then when the bullet enters the player apply your damage to them and kill the bullet.
Thanks i thought about this but then dismissed it, I have no idea how the tutorial ended up doing it, but this confirms my problem I'll definitely try this now.
Answer by XYHC · Jul 30, 2021 at 02:28 AM
Set Bullet's rigidbody collision detection mode from "Discrete" to "Continuous" inside the inspector if the bullet travels very fast.
Your answer
Follow this Question
Related Questions
OnTriggerEnter2D fires multiple times when Animator component is on 3 Answers
BoxCollider error from C# to UnityScript 0 Answers
How do you get OnTriggerEnter2D to activate only once? 3 Answers
OnTriggerEnter2D is called 2 more times in Melee combat system 1 Answer
how to make AI change from patrol to chase within a certain range 1 Answer