How can I always assure that my gun shoots strait where it's pointed?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I have been trying desperately to make a gun that shoots in the direction the barrel points

Disclaimer

It’s an issue when the player animates or runs (Specifically running since I’ve animated over arm movements), as the barrel flails rather than shoots straight in front

but I haven’t come to a reasonable solution. I am trying to make a gun that has the ray starting at the barrel and projecting straight forward from where the character faces

  1. What is the issue? Include screenshots / videos if possible!

I have a few issues actually.

The first one would be this one:

As you can see, the ray often strays towards some sort of point that I haven’t made. As I turn you can see the ray going towards that imaginary point. I’m trying to find a way to stop that from happening as it affects gameplay. The ray doesn’t always go towards the players mouse (Which should be straight in front of them due to the forced shift lock) and It becomes problematic as seen here:

(This seems to happen with mouse.hit, viewpoint and screenpoint)


(Everytime the output moves the shot is registered)

Once again, a simple solution would be to stop relying on the mouse or screen, but that brings up the issue of when the player walks or their character animates a movement that’d put the gun off target a little.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried screenpoint, viewport point, mouse hit, and shooting from the barrel, but all have been proven faulty. I’ve also looked around but i honestly don’t know what to search for.

If anybody can help, give suggestions or make corrections, I’d appreciate it!

4 Likes

This is a classic problem in FPS game design, and AFAIK all solutions are a compromise. You can’t have both:

a) bullet follows the direction towards the mouse (OR the camera)
b) bullet follows the direction of the muzzle

If you care more about gameplay than visual fidelity or realism, it’s common to have the “real” bullets exit from the center of the camera in the exact same direction as the camera, so that shots always land exactly where the player is aiming with the crosshairs in the center of their screen.

The moment you shift the origin point of the bullet, this becomes impossible because nearer and more distant targets can overlap perfectly on the center of the screen, so it looks like if you line them up the bullet should hit both targets. But for the bullet to actually go towards the target from some off-center point, two different directions are required.

This is known as parallax, and unless bullets shoot out of the center of the screen then you HAVE to consider it. I’d wager that is what’s causing the issues in the 2nd video.

The issues in the 1st video is probably because the mouse is hitting the character when you zoom out. Do a blacklisted raycast or set mouse.TargetFilter to include the player character.

5 Likes

I don’t see why you would want it going straight out of the barrel, sure it is more realistic but this could cause problems with aiming unless the gun barrel is extremely lined up with the mouse.

4 Likes

ok so i decided to go with the “Ray from camera to object hit” and it’s showing improvements but the ray still strays off towards the right or left and I’m not sure why. (I could send code if needed)

2 Likes

I wanted to go for realism but if i’ve learned anything from games, I’ll have to fake the bullet going out of the barrel, which shouldn’t be too hard since I have a shift lock on the player

2 Likes

Sure, send the code if you’re comfortable with it. Try printing which object gets hit, and compare that to what you expect to happen. Try putting a part where the mouse points and see if that is as expected.

1 Like

The way games like Phantom Forces does it is the “realistic way”, where bullets always leave straight out of the barrel of the gun, no matter where it’s pointed. This means you can never rely on the crosshairs in the center of the screen, and consistently have to aim up and to the left to actually hit anything in hip fire mode. This is a fine game design choice, and even leads to some fun moments, but it is hard for new players who have plated a lot of FPS games to get used to. You can really see this for yourself if you equip a laser sight on any gun in the game and try shooting while sprinting. It does mean that if aiming down sights should feel good to the player, you have to make that close to perfect in terms of having the rear and front sights lined up in the middle of the screen to avoid parallax issues. It’s not especially hard, but a bit more involved than what I’ve seen most tutorials go into.

2 Likes

ok! I’ll send the code via private message

I see, and yeah i’ve noticed in some games the laser does move as if it’s from the barrel. I think I’ll go with the cursor point because like you said it may be eaiser for newer players. Maybe I’ll explore barrel shooting later on in my game’s development!

2 Likes