Bullet Collision not consistent?

My bullets aren’t detecting collision consistently, infact its every other bullet registers as a collision.

I have a rigid body on the bullet and have tried applying a rigidbody to the object but it gives the same results with or without.

The code below shows the collisions working, I have tried a linecast and a spherecast but both don’t seem it improve anything. However I know the issue is due to the bullet travelling too fast but what bullet travels slow? :stuck_out_tongue:

Is there any workarounds for this? I’m having no luck browing the forums

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

public class BulletControl : MonoBehaviour {

    [Range(0, 50)]
    public float bulletSpeed;
    public float deactivateTimer;
    float tempDeactivateTimer;
    bool bulletHasHit = false; // bullet has hit something
    void Start()
    {
        tempDeactivateTimer = deactivateTimer;
    }
    Vector3 prev = Vector3.zero;
    // Update is called once per frame
    void FixedUpdate()
    {

        if (!bulletHasHit)
        {
            BulletHitDetection();
        }
      //  transform.Translate(transform.forward * bulletSpeed);

    }

    void LookForCollision()
    {
        Collider[] hitDetection = Physics.OverlapSphere(gameObject.transform.position, .5f, 1 << 9, QueryTriggerInteraction.Collide);



        if (hitDetection.Length > 0)
        {
            float closestObjectDist = 10000f;
            GameObject closestObj = hitDetection[0].gameObject;

            for (int i = 0; i < hitDetection.Length; i++)
            {
                if (Vector3.Distance(gameObject.transform.position, hitDetection[i].transform.position) < closestObjectDist)
                {
                    Debug.Log(hitDetection[i].name);
                    closestObjectDist = Vector3.Distance(gameObject.transform.position, hitDetection[i].gameObject.transform.position);
                    closestObj = hitDetection[i].gameObject;
                }
            }
            BulletHitObject(closestObj);
        }
    }
    public void BulletHitDetection()
    {
        RaycastHit hit;
        if (!bulletHasHit)
        {
            LookForCollision();
            if (prev != Vector3.zero)
            {
                if (Physics.Linecast(prev, transform.position, out hit, 1 << 9, QueryTriggerInteraction.Collide))
                {
                    BulletHitObject(hit.collider.gameObject);
                }
            }
            else
            {
                prev = transform.position;
            }

        }



    }

    public void BulletHitObject(GameObject hitObject)
    {
        bulletHasHit = true;
       // Debug.Log(bulletHasHit);
        if (hitObject.tag == "Wall")
        {
            Debug.Log("Wall");
        }


        this.gameObject.SetActive(false);
    }


    void OnDrawGizmos()
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawSphere(transform.position, .5f);
    }
}

Bump, been trying with a simple raycast too and still ignores collision 90% of the time… anyone know why?

            if (Physics.Raycast(transform.position, -transform.up, out hit, .5f, 1 << 9, QueryTriggerInteraction.Collide))
            {
               BulletHitObject(hit.collider.gameObject);
            }

raycast and linecast are the same thing.

try removing the layermask and querytriggerinteraction enum from teh call.

Side note… I would not do a overlapshere call. If you were going to do that, do a SphereCast instead of a ray/linecast.

In your BulletHitObject method, you are disabling the bullet. I would destroy it to clean up clutter in your scene. (unless you have a specific purpose for that.)

Just fixed the problem from casting a linecastfrom the bullet start pos to its current pos until it hits an object,

The bullet deactives due to ObjectPooling so it reuses the same bullets so i don’t have to instantiate every time :slight_smile: