Why is my projectile not shooting straight near my character?

I have got a little crosshair on my screen and want the projectile to be shot at where-ever the crosshair is pointing. If the max speed is set high enough, it’ll go where-ever the crosshair is pointing at. But, when I aim at somewhere close to my character, it will shoot to the right of it.

Within the ProjectileMovementComponent settings, set gravity scale to 0. That will make sure your projectiles move in a straight line and is not affected by gravity.

To shoot exactly at the cross hair, however is a little bit more involved. The trick is to find a vector towards the world position of the cross-hair and set this as the rotation for your projectile. You can use Converted Screen space to world space node to get the world position of the cross-hair.

The problem is that the projectile does shoot straight, say for example if I was not looking at the ground (and was looking at the blue sky). It would then shoot perfectly straight to where the crosshair is.

But, if I look at or near the floor for example, it would sorta get stuck before being able to reach where the crosshairs are (as shown in pic).

Perhaps your projectile is colliding with the Capsule Component of your character?

Within your projectile’s event graph, override ‘Beign Overlap’ and ‘Hit’ and print out the name of the Actor that is blocking it (if it is indeed blocked). That way we will klnow for sure.

Also try to offset the starting point for your projectile a little bit away from your character (and gun).

Another thing you could try is to use the ‘MoveIgnoreActor’ node to specfically tell the collision component to ignore your character.

Let me know what you find

Okay, I have tried to set a custom collision preset for my character and the projectile to ignore each other (pawn/projectile). This doesn’t seem to work. And judging by this new screenshot, it is not the character capsule that is blocking the projectile. Instead, it is the location the ball is shot from (ball gets stuck to floor a distance away from character).

It only seems to go towards the crosshair dot when I have it at a certain angle. If I aim way too high (eg. aiming at the sky), it the projectile eventually travels above the crosshair dot. If I aim too low (eg. directly at floor/feet), it will spawn/hit/collide with the floor to the right of me.

I think having the “Gun offset” to determine where the projectile is going to spawn and eventually travel is not going to work at all. Any way round this?

Edit:

Check last comment for Blueprint with answer


Original Answer:

From the comments in the thread and your blueprint, it looks like you are doing this:

Getting the spawn location for the projectile by adding the GunOffset to the current position of the Pawn. You are then assigning the rotation of the projectile equal to the rotation of the Controller, basically your Camera. Then, you spawn in the projectile and give it some velocity in the direction it is facing.

Depending on your GunOffset, you will get different results because you are not compensating for the difference in position with respect to your Camera and your projectile spawn position.

In order to achieve what you are looking for you have to compensate the projectile rotation to account for the difference in position from your projectile spawn point and the camera position. What I’ve seen people do is use a Line Trace from the Camera position to get one of two things:

  1. Get the position of a collision within a certain range (weapon range) and use that position to correct the projectiles initial velocity vector. (LineTraceHitPosition - ProjectileSpawnPosition).Normalized * ProjectileInitialSpeed will give you an initial velocity vector for the projectile to have it “Shoot” toward the point where your Line Trace hit.

  2. Get the position of some default max distance for the weapon in the direction of the camera look vector (your crosshair) and use the same logic as number 1 to get that point and correct the projectile initial velocity vector.

Hopefully this helps you get going with it! =)

I have used this or similiar methods in my previous games (including one on unity) and i can say that this is a very good way to fix the problem.

Hi Knovolt,

That is exactly what is causing this, the gun offset is set slightly to the right to compensate for the gun position on the arms. You will have to adjust the numbers or add a “find look at rotation” to cause the projectile to fire at a different angle than you currently have.

^this

And some-else has posted a more detailed solution above me. Check that one out.

Sorry, from the first image you posted, it looked like the ball was sticking onto the gun - not the floor. Lack of perspective, I suppose.

This is really helpful. Thanks

I’m trying to apply this logic in blueprint but clearly lack the skill yet to do so
Any advice or (even better) showcase you could share please ?

Edit: Trying to figuring out this by myself I just found out there is a “convert screen location to world space” node.
Am I wrong by thinking this would be a shortcut ?

Edit 2 : never mind, I just understood my mistake…the "convert screen location to world space"does not take into account the geometry (i.e your level) therefore it’s not a replacement for a line trace…

You cannot have exactly what you want with the constraints that are currently expressed.

You have the choice of either making the projectiles look like they come out of the gun, which will cause an offset from the camera-screen-center as long as the gun is off center, or making the projectiles follow the camera-screen-center, which will make the projectiles not look like they come from the gun.

You can change the animation to put the gun in the center of the screen – straight in front of the camera. If you do this, the projectiles will follow the crosshairs.
Or you can spawn the projectiles based on the camera ray, rather than the gun offset. This will send the projectiles in the direction you want, but will perhaps not look good.

Hello jwatte

Would it make sense to draw the crosshair on the hud not using the camera ray but the gun ray ?
I assume it would mean not getting the crosshair at the center of the screen but I suppose bullets and crosshair would now always be aligned right ?

No, you can’t do that, because for any ray through the screen, it will be traced from the camera position through that pixel on the screen. That’s what it means to be the camera :slight_smile:

If you picked any particular place on the screen, but still fired from the gun, the projectile would be “hither” the crosshairs when closer than some particular depth Z, and would be “yon” the crosshairs when further away from than that Z. The particular Z depends on the position you choose, and how the camera ray through that point intersects the actual projectile ray.

The simplest fix is either to make the gun animate to the center of the screen (“sniper aim mode” style) or to fire the projectile not from the gun, but from the camera.

I understand, thanks for taking the time.

I see the easy workaround would be to fire from the camera, use a good muzzleflash, animation etc…to “sell” the illusion to the player.
Unfortunately for me, I want a nice trail leaving from the gun and I want the gun to feel deadly accurate without iron sight so what if there is no crosshair on screen but I use a “laser sight” attached to the gun (i.e the socket) ?
This would be equivalent of drawing a (visible) cast from the gun isn’t it ?

It would automatically adjust to the Z and the player will always know where he aim exactly with his gun (that what it looks like in my head right now anyway :slight_smile:

Adding this pic for the sake of clarity (english is not my native tongue)

For blueprint you would do this inside your “Pawn” blueprint. You would grab the “Camera” component and use it as a variable in the blueprint. You would then use this components “Forward” vector to draw a line trace in that direction and you decide the length of this line trace. You use the return value from the line trace node to get information about the hit, if there is any. You then use the “Weapon Offset” in the blueprint to calculated the new velocity vector for the projectile.

I’ll be able to help more this evening if you need it. I apologize for the delay in my response… let me know if you need more guidance =)

Yes, a laser sight would solve your design, assuming that works in your world.

Another option is to add a helmet-visor HUD that draws a target indicator at the spot in the world where a ray-cast from the gun hits something.

Thanks that’s invaluable help !

This is where I am right now.
Is this part ok ?

  • Should I use trace channel “visibility” or “camera”. (my guess is for camera).

  • I’m not sure at the “break hit result”, I’ll admit my mind get tangled as soon as vector comes in…Should it look like that ? (with 3000 being the projectile speed)

Thanks, I’ll try this as well and good suggestion for the helmet-visor HUD.
You gave me an idea for something to experiment :slight_smile:

Do you still want help on this man? I saw you accepted the other answer as “Solved”. I will say though, his method is simply a work around. Doing it the way I suggest will let you play in any perspective (3rd, 1st) and place the camera and weapon anywhere you want. Just FYI, this method is widely accepted for what you are trying to accomplish. (It is possible with your current constraints)