Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

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.alt text

Product Version: Not Selected
shoot.jpg (124.1 kB)
more ▼

asked Aug 17 '14 at 07:35 PM in Blueprint Scripting

avatar image

3 4 7 6

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

3 answers: sort voted first


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.

alt text

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.

alt text

Hopefully this helps you get going with it! =)

more ▼

answered Aug 18 '14 at 02:41 PM

avatar image

1.6k 61 45 237

avatar image Borzi Aug 18 '14 at 02:54 PM

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.

avatar image Max4dmoon Aug 24 '14 at 04:27 PM

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....

avatar image AnxGotta Aug 25 '14 at 11:50 AM

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 =)

avatar image Max4dmoon Aug 25 '14 at 04:49 PM

Thanks that's invaluable help !

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

  • 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)

alt text

capturebp-wip.jpg (123.1 kB)
capturebp2-wip.jpg (92.2 kB)
avatar image AnxGotta Aug 25 '14 at 10:23 PM

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)

avatar image Max4dmoon Aug 25 '14 at 10:29 PM

Hey AnXgotta

I'm not the one who asked the question in the first place so no, it's not me who accepted the other answer (I think only the owner of the topic can do that anyway).

I'd like to try every option and yes I'm still (in very) need of your help please :-)

avatar image AnxGotta Aug 25 '14 at 10:46 PM

Ok so what you can try is a Single Line Trace for Object and get break the hit result like you do. This node will return the first object you hit, if any... be sure to Ignore Self. If you hit an object, get its location from the HitResult and use the formula I posted above to get the Velocity vector for your projectile. You only use the GunOffset for the spawn location of the projectile (and the formula above) to have the projectile fire toward where you were aiming. If you get no HitResult from the Line Trace you just use the location of your max distance for the trace hit... Camera world location + Camera.Forward * MaxDistance instead of the Hit Actor world location.

avatar image Max4dmoon Aug 26 '14 at 01:05 AM

I really sucks at this....at least I have my stubbornness for me

Ok this is what I have right now ! alt text

I think Part 1 is ok (in red), I know you told me to use Single Line Trace for Object but I don't know how I should handle the "Object type" requested by the node however using the Trace for Channel and using the Draw Debug I can see than I'm casting a collision trace where it should be (hitting the crosshair and the first object encountered). So at least I know I get the right value here.

Part 2 is me trying to apply your formula : (LineTraceHitPosition - ProjectileSpawnPosition).Normalized * ProjectileInitialSpeed (which is 3000 here). Let me know if it's ok.

Part 3 is where I mess up for sure....I have to admit, I'm not sure what does this mean : "Velocity vector for your projectile" so I'm improvising and no surprise it doesn't work.

Sorry to force you to hold my hand and thank you for your infinite patience !

avatar image AnxGotta Aug 31 '14 at 02:29 AM

Alright man, here it is using a modified First Person Blueprint Example

First, I added a Scene Component called "ProjectilSpawnPoint" to the player blueprint to help with finding the vectors and getting the world position to spawn the projectile. I put this Scene Component as a child of the "Mesh" Skeletal Mesh Component so that it would follow the rotation of the player.

alt text

Next, is the blueprint. I use the Camera Component, Projectile Spawn Component, and a few other variables to do this. The MaxRange variable is an integer variable I have set to 3000... you can make it whatever you want the max range to be. I also made a variable called MaxRangeHitPoint as a Vector that gets assigned as the world position of the point of the max range of the weapon. Look at the blueprint for the rest and let me know if you need any more help.

alt text

avatar image Max4dmoon Sep 03 '14 at 02:51 PM

Wow...it's clear, it works and I understand it ! You're a very good and patient teacher.

/bow /thanks

avatar image AnxGotta Sep 05 '14 at 12:08 AM

No problem, I'm glad it helped =)

avatar image TK3D Oct 25 '14 at 12:41 AM

This was extremely helpful. Just wanted to let you know, AnXgotta, you rock! :D you helped me more than once already.

avatar image kayrk Aug 17 '17 at 07:33 AM

I know this is a super old thread but I have a similar issue where I can toggle from FPS and TPS and my crosshair is not in same place so I need to do something similar to position crosshair and shoot to the crosshair so it hits same point no matter which view you are in. Would you be able to help AnXgotta?

avatar image AnxGotta Aug 17 '17 at 04:14 PM

Hey kayrk. If you are using the same camera and simply moving it on perspective change, then yes this code will work fine. If you have two cameras, be sure to use the currently active one to get the values for the position and rotation.

avatar image kayrk Aug 20 '17 at 01:01 AM

I got it all working. Appreciate it.

avatar image kayrk Aug 22 '17 at 06:43 AM

Well I thought I had got it solved but ran into a issue. I posted about it over here https://answers.unrealengine.com/questions/698683/first-person-third-person-aiming-discrepancy.html if you have any insight AnxGotta

avatar image zolaa Nov 21 '14 at 01:45 PM

Hello! my bullet spawns at the spawn location, but isnt moving anywhere. i can walk into it and it damages me. :D

avatar image BaldDude Oct 02 '15 at 10:41 AM

Came up with a better version of your BP. In both "Vector - Vector" connect the "Impact Point" node from "Break Hit Result" and the projectile will always hit a specific object instead of the variable distance you set . Hope this helps anyone else . Cheers!

avatar image CamWitWizard Aug 13 '18 at 01:14 PM

It might sound stupid but I'm not quite sure how you would get the gun offset for the variable you made (I assume its a variable) Do you find the vector between the pawn and the end of the gun perhaps?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

more ▼

answered Aug 18 '14 at 04:43 AM

avatar image

3.4k 154 51 271

avatar image Knovolt Aug 18 '14 at 09:39 AM

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).alt text

bprint.jpg (224.6 kB)
avatar image mindfane Aug 18 '14 at 11:06 AM

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

avatar image Knovolt Aug 18 '14 at 02:09 PM

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). alt text

shot.jpg (149.2 kB)
avatar image Knovolt Aug 18 '14 at 02:31 PM

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?

avatar image Adam Davis STAFF Aug 18 '14 at 08:26 PM

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.

avatar image mindfane Aug 19 '14 at 03:01 AM


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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

more ▼

answered Aug 24 '14 at 05:48 PM

avatar image

1.3k 48 41 88

avatar image Max4dmoon Aug 24 '14 at 07:53 PM

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 ?

avatar image jwatte Aug 25 '14 at 12:02 AM

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 :-)

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.

avatar image Max4dmoon Aug 25 '14 at 10:35 AM

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 :-)

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

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

avatar image jwatte Aug 25 '14 at 03:10 PM

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.

avatar image Max4dmoon Aug 25 '14 at 04:52 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question