Need help finding a way to get a consistent bullet collision

I have a prefab of a bullet being shot from a gun attached to the player.

The bullet is flying fast enough for there to be a few problems with the oncollisionEnter detection.

I made a debug.Log(transform.postion) in the oncollisionenter and the bullet is reading a different vector each time it runs the debug

I’ve tried making a raycast that’ll check in front of the bullet and find a spot to collide with, but I wasn’t able to get that to work either.

If anyone knows how to check for a consistent collision spot on a fast moving bullet can you please help me?
Thank you.

Don’t use a monobehavior for each bullet, it’s not gonna scale well.
What was your problem with the raycast? instead of saying “it didn’t work” say what didn’t work, show your code.
I always do bullets with raycasts myself so i know it works.

2 Likes

Sorry I was at work all day today. what should I do instead of having it call from Monobehaviour?

For the raycast not working, I have a raycast on the bullet that checks to see if there is a wall in front of the bullet to check if there will be a collision because the bullet is moving so fast sometime the bullet skips through the collision and collides at a different point on the collider it is hitting.

For the code I have attached to the bullet, this it it. I changed a few things after I posted this yesterday and I’m not to sure where I left off so if there are some pieces that dont make sense, that’s probably where I left off.

[SerializeField]
    private float bulletSpeed;

    private int bulletDamage;

    private bool destroyOnNextFrame;

    Vector3 distance;

    public int damage
    {
        get => bulletDamage;
        set => bulletDamage = value;
    }

    private void Start()
    {
        Destroy(gameObject, 2);
    }

    private void Update()
    {
        if (destroyOnNextFrame)
        {
            Destroy(gameObject);
            return;
        }

        distance = transform.forward * bulletSpeed * Time.deltaTime;
        RaycastHit hit;



        if (Physics.Raycast(transform.position, transform.forward, out hit, distance.z))
        {
            Debug.Log("Hitting shitz");

            transform.position = hit.point;

            ApplyDamage(hit.transform);

            destroyOnNextFrame = true;
        }

        else
        {
            transform.position += distance;
        }
    }

    private void ApplyDamage(Transform hitObject)
    {
        HealthBehaviour health;

        health = hitObject.transform.GetComponent<HealthBehaviour>();

        health?.ApplyHealthChange(bulletDamage);
    }

   

    //private void OnCollisionEnter(Collision collision)
    //{
    //    Debug.Log("Hit with collision");
       
    //    ApplyDamage(collision.transform);

    //    Destroy(gameObject);
    //}

    //private void OnDestroy()
    //{
    //    Debug.Log(transform.position);
    //}
}

I want the raycast to check in front of the bullet and look for a collision so the bullet will always collide and be destroyed at the same spot (if the player and bullet angle doesn’t change)