2
votes

i'm actually trying to make a sort 2d game with unity, in this game we control a character that can shoot bullets with a weapon. Actually an "Uzi", so it shoot very fast and hard,(so, a gun) the problem is, that sometimes, depending on the distance with the ennemy, the bullets go trough the box collider of the ennemy, and so don't touch it. I think it has to do with the Update/ Fixed update system of unity, but i'm not sure. and The bullets are already in collision detection Continuous. There is a part of the code, and screenshots.

 void OnTriggerEnter2D(Collider2D target)
{
    if (target.gameObject.tag == "FirePoint")
    {
        Fire();
    }

    if (target.gameObject.tag == "Building")
    {
        Destroy(gameObject);
        //print("Don't shoot on the walls !");
    }

    Ennemy enemy = target.GetComponent<Ennemy>();
    if (target.gameObject.tag == "Ennemi")
    {
        enemy.TakeDamage(damage);
        Destroy(gameObject);

    }

}



void Fire()
{
    GetComponent<Rigidbody2D>().AddForce(transform.right * bulletForce);
}

and there is the method for shooting, in another script.

public void Shoot()
{
    DispersionDesBalles();
    nextFire = Time.time + 1f / fireRate;
    Instantiate(Balle, spawnPoint.position, fireRotation);

    ballesRestantes--;
}

and screenshots of the bug Here

The red rectangle are ennemies, and the little green things are bullets, don't pay attention to the graphics :D, It's just for testing.It's pretty annoying because in the game, the bullets are less spaced apart, but it amounts to the same thing.

I hope someone can help me. have a Nice day !

1
Play around with the „Continous Dynamic“ mode, not only for the bullet but also for the Enemy. - JeanLuc
I will try, thanks - Alex Soti
„Continous Dynamic“ is expensive and you should avoid it if possible. If you can make it work by changing size of colliders, it should be better for performance. - Dave

1 Answers

1
votes

Yes, it has something to do with the physics engine refresh rate which is not in sync with rendering. In case of small and fast colliders you will get issues like this. There are different ways to deal with it but the easiest would be to increase your bullet collider size to make sure it is not passing trough.