Download

Using Traces for Weapon Bullets

Hey guys,
I’ve been slowly learning a lot while building my game prototype. I have projectile’s spawning towards the mouse cursor, but that’s not terribly practical or pretty, and I need a new system. So I’ve been trying to use a trace for that “instant hit” style gun. I’ve got some issues though…

My trace results are weird… It’s almost like it’s going from the camera/mouse to the character/model or vice versa. I’m not sure, but they’re definitely not going from the character to where the Crosshair is pointing. I’ve posted a blueprint and screen shot of my trace results. I’m not sure what other information to provide at this point.

Any help is greatly appreciated!

edit, if I remove the “rotate vector” from Get Actor Location, and plug “Get Actor Loc” into “Start single line trace” I basically get the same thing.

So I found another post that linked me to here: Shooter Game Tutorial: Prototype - Rail Weapon (another forum user’s site, sorry I forget your forums name right now).

I think that works because it’s first person and the socket rotation is always in line with the screen and crosshair, so there’s less variables. I know I’m missing or misunderstanding something about how the vector, trace, and mouse all work together to get what I’m looking for, I just don’t know enough to know what I’m missing, if that makes sense.

Anybody have this problem or know how to point me in the right direction?

I’m willing to pay a small-ish fee if some blueprint master can help me. Just respond to this thread with your contact info.

Doing this actually requires TWO traces.

The reason is because the crosshair is not in a 3D space, it’s in a 2D space, its coordinates only exist in 2D space. What the crosshair is “looking at” is dependent on how far away it is from the screen; you have to project the 2D screen coordinates forward until you hit an object which can be “looked at” to get the 3D location of that object.

What you need to do is trace outward from the center of your screen for some extremely long distance (say 15% or so further than the trace you’ll do for firing your gun, to account for the angle introduced by the camera) and then that hit point will determine “what I am aiming at”. This, incidentally, is how many games do it; you’d be surprised how many TPS games simply fire outward from center screen, rather than from the gun/player character itself.

If you want to instead trace from the gun barrel to the center of the screen (so that the player’s shots can be obstructed by walls or other obstacles that are between him and the target, even though the offset camera can see past them), you need to FIRST trace from the center of the screen outward, then perform a SECOND trace from the gun barrel location to the hit location of the first trace.

I do something similar in my game (though instead of a second trace, I launch a projectile from the gun barrel toward the “looking at” location using the LookAtRotation function). Here’s a screenshot of the set-up.

e2fba77bde27bc06a0189f3207ce11872f71bfc5.png

Note that I get the screen size and divide it in half across both dimensions to get “center screen”, which is my reticle position. Then I convert the screen space to a 3D vector, which takes “center screen” and tells me where it exists in the world (based on the camera position), and then projects outward to determine what the reticle is laying on. If you use a different aiming system than standard TPS (for example, a freed cursor which is not locked exactly to center screen) you could do something similar by taking the cursor position, BUT REMEMBER! The 3D world position has a horizontal and vertical position, and its depth is equal to the CAMERA! The game is trying to trace to a place on the camera itself, not what that camera sees. If the camera is behind the player, as in your project, tracing from the player actor to a camera position will trace backward to the camera itself. This appears to be what your project is doing; spawning traces that run from the player back toward the camera and hit where the camera is.

Also note that I spawn an actor with projectile movement; where I spawn my bullet, you would instead perform a second trace, with the start location being your player (or gun barrel socket, whatever) and the end location being THE HIT RESULT OF YOUR FIRST TRACE.

And here’s an example of the trace in action with debug drawing. The only reason the trace line in the first image is slightly off-center is because I have a camera recoil attached to my shot firing; by default you essentially cannot see the trace, since it goes straight out from the camera. It’s only from another angle that you can see how the trace is being drawn, from where the center of the camera is in 3D space outward in a straight line to what the reticle is over. It’s this trace that determines what the bullet projectile is launched at (or the end of your second trace).

TraceExample.jpg

Man you are a life saver. I’ve been going nuts trying to figure this out! Thank you so much.

Here is an easy way to trace to the crosshair at the center of the screen.
traceHit.jpg

Thanks for the response, could you explain a little bit about the camera player manager? Is it basically the same as getting the view port?

Hi zlspradlin,

Did you figure this out, if so can you pls, pls post a screenshot of your blueprint here, i am still having problem make it work

Thanks

Man. I know this is an old thread but it just helped me immensely. I should have read your text instructions more clearly. I just took your example pic and tried to modify it on my own… not realizing you specifically said what to do in the text below. Coulda saved me an hour or so… silly me. Trying to get the gun to fire at crosshair, instead of just at the centre of screen and then start the trace from the gun rather than the camera… didn’t realize it was a two trace approach. This worked perfect once I read more clearly.

The converting 2d space to 3d is a huge missing piece (obviously). Thanks for the knowledge!

If you’re trying to do this… here is the part to really pay attention to from RythmScript’s post/pic. "Also note that I spawn an actor with projectile movement; where I spawn my bullet, you would instead perform a second trace, with the start location being your player (or gun barrel socket, whatever) and the end location being THE HIT RESULT OF YOUR FIRST TRACE."

Use the pic up until the FIRST trace… then create a second trace from the sequence 1. The hit result of your first trace location should connect to the END location of your new trace node… the start of your new trace node is simply your gun socket or whatever… (Get Socket Location…etc.)

you should see two lasers when you have the debug thing on… one from the camera behind the character and one from the players gun… this is fine since the first doesnt do anything other than find a location… the second is where the “bullet” stuffs would happen. (once you make it so)

Actually, since making this post, I’ve updated my system.

You may notice, if you’re observant, a Magic Bullet flaw with this system; if an enemy or object is BETWEEN the gun barrel and the camera (say, standing immediately next to the player), the bullet will actually fire BACKWARD out of the gun and hit the enemy!

What I’ve done now is modified the start location of my Reticle-Finder first trace to share a local-space X coordinate with the gun barrel socket. So when you trace outward to find what the reticle is on top of, that trace’s starting “into the screen” depth is not where the camera is, but where the gun barrel is. This way, objects behind the gun barrel cannot ever be considered what the reticle is resting on.

I’d suggest the weapons blueprint system on the Marketplace. That’s what I used as a base to make my own projectile system :slight_smile:

It seemed like people were complicating a very simple act, but then I found out GetActorEyesViewPoint() isn’t callable by the Blueprint scripting system.
It returns all the info you want for a basic trace like this.
No mess, no fuss.
But again, for some reason, its not setup to be blueprint callable :frowning:

hmm… yeah i see what you mean. its hard to catch without the laser on to see the start and end… because otherwise one would just assume the character is aiming that way but theres no animation for it pointing that close or something. I couldnt get to actually hit another character in the scene since the crosshair is always too forward from the player… as seen here

2015-03-29_142913.png

but i could finally recreate it against an object here

2015-03-29_142957.png

How do you make it share same local space on x as the gun barrel. I tried forever yesterday making the start point integrate with the gun barrel socket when i was trying with a single trace. I kept getting either it would hit the crosshair properly but start from the camera(top right behind) or it would hit the centre of the screen (not always the crosshair depending on distance) and originate from the gun barrel. Here is my current blueprint.

2015-03-29_142024.png

Next step is figuring out how to make this cause damage and kill something… right now I just have explosion particle playing where it lands to see it. It doesn’t actually affect anything. If you have a good starting point for this for me here you dont mind sharing… it could save me another day of learning… lol

Thanks.

Does it use line traces?

I initially set up a projectile system using the FPS projectile tutorial… but it seems like its more resource intensive to spawn a bullet every time and launch it when its just going to be invisible anyway.
plus the gravity/physics and speed weren’t lining up right for me… but I don’t know if I was doing it all correct. Seemed like the line trace method was the most practical for the speed of a gunshot. Figured Id use the other method for something like a grenade or whatever where you actually want to see the item flying through the air and bouncing around.

Weird it wouldnt be in blueprints… I would think firing is one of the most common things done in a game from unreal. Since a lot of games are shooters… yet I’ve been finding it very difficult to find examples on how to fire a weapon, make it hit something and make it do something when it hits them etc.

thought id post an update.

This worked initially… and loooked good… unfortunately now that ive progressed… I’m trying to do head shots and targeting hits when shooting and this only hits about 80% of the time… the rest it just doesnt hit the mesh.

I tried from the initial line and it hits everytime so theres an issue for sure calling the second line off the first one.

So im back trying to figure out how to hit the crosshair form the barrel…
since now i can either hit the crosshair from the camera… or start it from the barrel but it doesnt go to cross hair…

figure this would be standard settings

frustrating.

If anyone knows if this isnt a bug… let me know please.

7df181e9c2b1976784f121e061d767497ccd1099.png
line not connecting.png

I found the answer in another thread if anyone has this issue.
Though I don’t quite understand why it needs to be done.

From the Hit Result of the first line trace… drag out Impact normal and * it by a float of -1
Then drag out Location from the same node and + it by a vector (the impact normal *-1 result)
Then the result of that equation, add to the end point of the second trace.

While I understand what impact normal is… i really dont understand why reversing it and adding it to the place it should impact to begin with is necessary.

sigh

Thread source: https://forums.unrealengine.com/showthread.php?27029-Single-Line-Trace-by-Channel-baffled-by-its-behaviour

Impact normal indicates which way is outward from the surface.

Multiplying it by -1 and adding it to the location of the hit is saying “wherever this hit occurs, aim one unit more TOWARD the object”. Essentially it prevents situations where glancing impacts won’t work correctly by correcting everything just a touch; instead of aiming at an object, you always aim one unit further toward that object.

Personally I’ve never found it necessary (I resolved such issues by making the collision sphere for my bullet actors slightly larger) but if you’re wondering WHY it works, that’s the reason. It helps to correct for traces which would otherwise just barely impact an object.

Though for this situation, what I prefer is not to trace TO the trace location, but THROUGH it. Once I had the impact of the first trace, I would use a “Find Look At Rotation” starting at my socket and ending at the trace impact, take that rotation, Get the XVector, and multiply that vector by some very large constant like 40,000… And add the result to my start vector to get the actual shot trace’s end vector.

The reason being, a bullet doesn’t travel from barrel to target; it just travels in a straight line. The first trace discovers the straight line from barrel to what’s-under-the-reticle; if that’s 900 units away or 9,000, it doesn’t matter. The bullet has an effective range of X units and that should be the trace.

IMO.

What if you don’t have any bullet actors? The end of the line trace is where the “hit” is… i don’t spawn any projectiles in this case.