Bullets go through colliders

Hello,

I am attempting to build a top-down shooter where the bullets will ricochet off the walls for infinity. I am having real difficulty getting my bullets to stop going through my colliders. I have done every Google search and tried different things:

  1. My walls are done using a Tilemap. I have tried the tilemap collider which didn’t work. I then removed that and added box colliders to the walls.
  2. I have added rigidbody 2D components to the wall colliders above and my bullet also has a circle collider 2D and rigidbody 2D. The Collision Detection for both the rigidbodies is set to Continuous.
  3. I had a go at changing the Project Settings > Time > Fixed Timestep which actually worked, but had a negative impact on my player rotation, so I had to remove it.
  4. I have read every thread I could find and watched countless YouTube videos on raycasts. Unfortunately, the majority of these are for 3D and as much as I try to manipulate the code examples, they either give errors or don’t seem to do anything at all.

I am really struggling with this. It seems like raycasts might be the solution, but I have no idea how to implement it and can’t find a suitable 2D tutorial about it.

Here is the code I have now, which is letting bullets through the walls, like water through a sieve!

The Player_Shooting script is attached to the player object

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

public class Player_Shooting : MonoBehaviour
{
    [SerializeField] private Transform _firePoint;
    [SerializeField] private GameObject _bulletPrefab;
    [SerializeField] private float _bulletForce;
       
    private void Update() {
        if (Input.GetButtonDown("Fire1")) {
            Shoot();
        }
    }

    private void Shoot() {
        GameObject bullet = Instantiate(_bulletPrefab, _firePoint.position, _firePoint.rotation);
        Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
        rb.AddForce(_firePoint.right * _bulletForce, ForceMode2D.Impulse);
    }
}

The Bullet_Bounce script is attached to the instantiated bullet:

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

public class Bullet_Bounce : MonoBehaviour {
    [SerializeField] private Rigidbody2D _rb;
    private Vector3 _lastVelocity;

    private void Update() {
        _lastVelocity = _rb.velocity;
    }

    private void OnCollisionEnter2D(Collision2D other) {
        var speed = _lastVelocity.magnitude;
        var direction = Vector3.Reflect(_lastVelocity.normalized, other.contacts[0].normal);
        _rb.velocity = direction * Mathf.Max(speed, 0f);
    }
}

If anyone could assist me with this, I would really appreciate it.

Many Thanks,

J

Have you tried setting the collision detection to continuous?

Hi,

Yes, collision detection in both the bullet and wall rigidbody are set to continuous, but the bullets still go through most of the time. Sometimes I manage to get a few ricochet before they break through, but they always eventually break through the walls.

Many Thanks,

John

Please try with tag

Hi,

I changed it to the following and it still doesn’t work. All the box colliders on the walls have the “Wall” tag applied:

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

public class Bullet_Bounce : MonoBehaviour {
    [SerializeField] private Rigidbody2D _rb;
    private Vector3 _lastVelocity;

    private void Update() {
        _lastVelocity = _rb.velocity;
    }

    private void OnCollisionEnter2D(Collision2D other) {
        if (other.gameObject.CompareTag("Wall")) {
            var speed = _lastVelocity.magnitude;
            var direction = Vector3.Reflect(_lastVelocity.normalized, other.contacts[0].normal);
            _rb.velocity = direction * Mathf.Max(speed, 0f);
        }
    }
}

hello john,

I am facing the same problem here too, have you figured out a solution?

Use raycasting. Rigid bodies are not made for small Bodies going several hundred meters per second

You just cast a ray from the previous location of the bullet to the current location checking for colliders.

2 Likes

Hi,

As mentioned above, I got around this by using raycasts.

Cheers,

J

Thanks!