Issue with direction of bullets being fired?

I have a script that fires bullets that I use with my first-person player, it uses gravity for bullet drop and I use a Linecast for the bullet positions plus a list to handle each new bullet.

However I just have an issue with calculating where the bullets should fire from and the direction. I have a “muzzle” empty object that I place at the end of my gun objects so that the bullets fire from the end of the actual gun. I also have a very simple crosshair centered on the screen, and the bullets fire at/towards it which is good.

But there’s a weird problem I just cannot solve, when I fire bullets near the edge of any collider, for example a simple cube object, the bullet direction will automatically change slightly to the right, and start firing in that new direction. So if I’m firing bullets at y-position 2.109f, if it’s next to the edge of a collider, it will then change direction and fire at y-position 2.164f which is very bad since it’s a large gap. I believe it’s to do with my Raycast and its hit calculation, but I can’t seem to fix it.

Any change I make either fixes that problem, but then bullets no longer fire towards the crosshair. So basically I fix one issue and break something else. I can post more code aswell.

void FireBullet()
{
    Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f)); // default unity docs line
    Vector3 aimDirection;

    if (Physics.Raycast(ray, out RaycastHit hit))
    {
        aimDirection = (hit.point - muzzle.transform.position).normalized;
    }
    else
    {
        aimDirection = ray.direction;
    }

    Vector3 spawnPos = muzzle.transform.position;
    activeBullets.Add(new Bullet(spawnPos, aimDirection, Time.time));
    Debug.Log($"fire bullet at pos {spawnPos.y}");
}

If I take out that entire Raycast if statement and just use “aimDirection = ray.direction;”, the problem of bullets changing position goes away, but bullets no longer fire towards the crosshair.

What i do not understand is that you are debuging the spawning position of your bullet, right?

Did you verify if the bullet lands correctly also?

If so, then, it could be because of the following:

Firing, from a point that isnt your crosshair, leads to objects being in the way of your bullet even though they aren’t for your raycast.

I’m not sure why it changes your Y axis though…

Yeah I see what you mean. I debugged the bullets landing and they seem fine (until the direction change). This is a picture I just took of Scene View as I’m firing. I have debugs to show the path of the bullets as well and added green bullet marks for testing. The bullets fired in the vertical line on the left all work fine. The second I reach the top of the wall, you can see the bullet on the right, which changes the direction of every future bullet (unless I manually move my mouse and fire elsewhere).

It always happens when at the top of a collider. I just tested it with no colliders in sight, the shooting works perfectly. Not sure if it helps.

I understood now.

Your ray.direction is the equivalent to a transform.forward vector from the center of your screen.
That direction, when your crosshair goes suddenly out of an object (and so really close to an edge after recoil) will be applied from the muzzle of your gun, directly forward.

Like this:

1 Like

You won’t solve it. You will choose a workaround that suits your game.

Aiming parallax / converging vs parallel lines of sight / lines of bullet: