Feed LineTrace random spread into SpawnActor spawn transform?

I have a blueprint with firing logic, where I’m using both a LineTraceByChannel node for hit detection and a SpawnActor node for bullet trail visuals.

I’ve managed to get the LineTraceByChannel to have a random spread.

How do I use the same LineTrace random spread to influence the SpawnActor Spawn Transform? How can I get their paths to line up?

You want to know the location of where the trail will start from and end at (I’m assuming this is a gun since we are talking about bullet trails), that would be the location where your trail starts and spawns. So for the location of the transform you either keep it the same if it’s the correct start location or you put your gun muzzle as a location (if you don’t have a way of getting the gun muzzle location you can just attach a socket or if it’s not a skeletal mesh some invisible object).

For the rotation you want to calculate the direction, there’s already a node that does that for you called “Find look at rotation”, for the start you use the same location that you plugged into the transform and for the target you use the trace hit/end location (you could either set the random spread to a vector variable before spawning the trail and use that variable for both the transform of the trail and the trace end location or move the trail after the trace and get the exact hit or end location by breaking the Out Hit of the trace). If your trail is done properly that should rotate it to the correct direction.

Cheers, that worked great!

For anyone looking into this in the future, here’s the blueprint;

(“find look at rotation” is the same node in both screenshots)

Make sure the linetrace happens before the spawnactor.

Discovered a minor issue with this solution. If there’s no static mesh for the LineTrace to hit, or if the LineTrace length is too short that it doesn’t touch anything then the bullet trail will target the world origin as the target end location.

Solution to this is to create a skybox out of 6 cubes with a transparent material. And then make sure that the Line Trace By Channel’s length is utterly massive that it always collides with the outer skybox.

Yep, you could use the trace end vector instead (from the bottom of the hit result), you’ll also want to move that SpawnActor node to be called after the line trace instead of before (just in case it screws something up because of order of execution). Have a branch between the line trace and spawn actor with blocking hit as the bool, on true spawn the trail using rotation from the hit location, on false use the trail end instead.