My projectile is passing through walls

My projectile is passing through walls - which is something I don’t want.

The destroy game object works when it hits a enemy but when it touches a wall it goes through it i dont use onCollisionenter2d because it is a trigger can someone help

public class Projectile : MonoBehaviour
{
    public int damage = 10;
    public Vector2 moveSpeed = new Vector2(3f, 0);
    public Vector2 knockback = new Vector2(0, 0);

    Rigidbody2D rb;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Start is called before the first frame update
    void Start()
    {
        rb.velocity = new Vector2(moveSpeed.x * transform.localScale.x, moveSpeed.y);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Damageable damageable = collision.GetComponent<Damageable>();

        if (damageable != null)
        {
            Vector2 deliveredKnockback = transform.localScale.x > 0 ? knockback : new Vector2(-knockback.x, knockback.y);

            bool gotHit = damageable.Hit(damage, deliveredKnockback);

            if (gotHit)
                Debug.Log(collision.name + " hit for " + damage);
            Destroy(gameObject);

            if (collision.CompareTag("Tilemap"))
            {
                Destroy(gameObject);
            }

        }
    }

    
}

Can you try to make the walls bigger or the bullet slower? Maybe if the wall is really small or/and the bullet is really fast the collision won't be seen due to the movement amount per fixed update being higher than the wall size!

–

4 Answers

4

Well, in the OnTriggerEnter2D method you are checking if the hit transform has a Damageable component so your wall won’t get inside the if statement.

You should edit the code as it follows:

private void OnTriggerEnter2D(Collider2D collision)
{
     if (collision.CompareTag("Tilemap")) //I suppose the Tilemap tag is used for the walls
     {
         Destroy(gameObject);
     }else
     {
       Damageable damageable = collision.GetComponent<Damageable>();
       if (damageable != null)
       {
           Vector2 deliveredKnockback = transform.localScale.x > 0 ? knockback : new Vector2(-knockback.x, knockback.y);

           bool gotHit = damageable.Hit(damage, deliveredKnockback);

           if (gotHit)
               Debug.Log(collision.name + " hit for " + damage);
           Destroy(gameObject);
       }
   }
}

i did exactly what u said still nothing

Does your wall have the tag of "Tilemap" as assumed by Patrickmol's code?

–

yes it does exactly with correct spelling

Are you sure the wall collider is marked as a trigger?

–

Hello @Joeymorgan158,

Is there a collision between your wall and your bullet?

private void OnTriggerEnter2D(Collider2D collision)
    print(collision.gameObject.name); // check if the wall name is displayed in the console
{

If not then you need to check your wall layer or check if your objects are interacting
(Unity - Manual: Interaction between collider types)