Bullet passes through enemies

Hello, I am a coding novice so I am sorry if this is something that should be easily fixed, but I was taking a Udemy course Unity 2d game development, and I am on lecture 6 near the end, and I am having trouble making the bullet actually hit enemies. On Udemy it is lesson 101 where we program the bullet behavior. For some reason the script recognizes that bullets should disappear when they hit walls, but not enemies. This is the code for my bullet script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
[SerializeField] float bulletSpeed = 20f;
Rigidbody2D rb2d;
PlayerController player;
float xSpeed;

// Start is called before the first frame update
void Start()
{
    rb2d = GetComponent<Rigidbody2D>();
    player = FindObjectOfType<PlayerController>();
    xSpeed = player.transform.localScale.x * bulletSpeed;
}

// Update is called once per frame
void Update()
{
    rb2d.velocity = new Vector2(xSpeed, 0f);
}

void OnTriggerEnter2D(Collider2D other)
{
    if(other.tag == "Enemy")
    {
        // bullets should destroy enemies
        Destroy(other.gameObject);
    }
    // Bullets get destroyed when they hit a wall
    Destroy(gameObject);
}

void OnCollisionEnter2D(Collision2D other)
{
    Destroy(gameObject);
}

}

Can anybody tell me what I did wrong? If needed I can provide the zip file for my game.

Thanks

Hi John,

Welcome to our community! :slight_smile:

First of all, please do not apologise for making a mistake. At some point, all of us were beginners, and even the best among us make mistakes all the time. If we apologised for not being perfect, we would not have any time developing our projects. As long as you put effort into your code, everything is fine.

That being said/written, on to your actual problem …

For some reason the script recognizes that bullets should disappear when they hit walls, but not enemies.

Both the OnCollisionEnter2D and the OnTriggerEnter2D methods destroy “this” game object. The OnTriggerEnter2D method also destroys the other game object if the other game object has got the “Enemy” tag assigned in its Inspector. To me, the logic of the code seems to be clear, so I suspect that the issue is in the Unity Editor. (Remember you can also look at the lecture code changes via the link in the Resources of each lecture.)

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? If the code does not work as expected, maybe the bullets or something else have got the wrong tag and/or script assigned? Or maybe the bullet has got a non-trigger collider attached? If you already configured the collision detection matrix, check if the relevant layers are able to interact with one another.

I hope this helped. :slight_smile:


See also:

Hello.

I think you are right about something being wrong in the editor. I seem to recall putting the bullet script in the player game object because that’s where I put my gun game object. I will fix this in about 2 hours (I have a busy schedule) and let you know if I have any other issues.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms