- Home /
Enemy bullets pass through my walls
I've been wanting to make a retro fps game for a long time and now, I finally have time to start it, so i looked up on the internet for tutorials to start understanding its mechanics and I found a whole series containing everything I needed to know. After following it very closely, everything seems to work but one thing. My enemies shoot bullets, those bullets have a 2D circle collider which acts as a trigger, and a rigidbody 2D set to kinematic. My walls have 2D box colliders and yet those bullets pass through the walls like they were nothing!
Here are a couple of screenshots of my scripts and editor:
This is the inspector for my bullet game object:
and here is the one for my wall :
and here is the script for my bullet : public class EnemyBullet : MonoBehaviour { public int damageAmount; public float bulletSpeed = 3f; public Rigidbody2D theRB; private Vector3 direction;
// Start is called before the first frame update
void Start()
{
direction = PlayerController.instance.transform.position - transform.position;
direction.Normalize();
direction = direction * bulletSpeed;
}
// Update is called once per frame
void Update()
{
theRB.velocity = direction * bulletSpeed;
}
void OnTriggerEnter2D(Collider2D other){
if(other.tag == "Player"){
PlayerController.instance.TakeDamage(damageAmount);
Destroy(gameObject);
}
}
}
Any help given is super appreciated and I thank you for taking your time to read this. Let me know if you need more details :)
Answer by Mitrovic_Z · Dec 17, 2021 at 04:47 AM
I FOUND MY SOLUTION!
My solution was to add this to the trigger void:
private void OnTriggerEnter2D(Collider2D other){ if(other.tag == "Player"){ PlayerController.instance.TakeDamage(damageAmount); Destroy(gameObject); }else if(other.tag == "Wall" || other.tag == "Door"){ Destroy(gameObject); } }
Answer by ryanlin138 · Dec 17, 2021 at 04:35 AM
You have to set the Rigidbody2D's collision detection mode to continuous because discrete collision will cause small fast objects to go through thin walls.
Hi just tried it! Doesn't change anything unfortunately...