Projectile Collision

I am trying to implement a shooting mechanic, I have created a machine gun and it shoots little projectiles really fast ect. The problem I am facing is that when the PlayerPawn and the Projectile collide head on, the PlayerPawn stops moving.

So I am trying to figure out the best way to implement this is. Right now I have them set to both block each other. Which understandably causes the PlayerPawn to be unable to walk forward while being shot form the front.

When I set either the PlayerPawn or the Projectile to ignore, nothing happens, the bullets go right through the PlayerPawn not dealing any damage.

What is the best way to implement shooting a projectile which collides with the character but allows the character to keep moving forward despite colliding with the projectile?

Thanks

Use LineTraceSingle!

Here are a few examples by our overlord Rama:

Trace Functions

Trace Functions draw a Line from a Starting Point to an End Point, colliding with the World.

What you want to do is draw a Line from the origin of the shot, out into the world.

You can specify if the Ray/Traceline should ignore specific actors/components etc or not.

Tip.: If you use a trace, make sure you look up “DrawDebugLines”

EDIT:

I know it’s not a “fix” for your solution, just a suggestion :slight_smile:

So with this solution instead of shooting a physical object you simply send out a trace that acts like a bullet and if it finds something it simulates a hit?

Yep, this is also what they used in the Shooter Example and I think it is common practice in the genre to use a LineTrace!
Getting the function to work may be a little tricky at the beginning (at least it was for me but I’m a total c++ noob) but if you don’t forget to draw debug lines, and exclude yourself from being hit by your own ray via parameters it will be fine!

You can also check out the function in blueprints, I’ve done it in both and its very easy in blueprints.

Okay, I actually have this implemented and working (so that is good news I guess). The 2 reasons I wanted to switch to firing an actual projectile

  1. Projectiles are subject to gravity, I thought this would make sniping more dynamic since the bullet will drop over time, so you’ll have to aim higher.

  2. The projectile will actually take ‘x’ amount of time to reach its destination, making it possible to dodge projectiles.

Is there a way that these two mechanics could be implemented using just traces?

I don’t think that’s possible. (But I’m a noob)

You could stick with your first solution, make the bullet not block anything and add a “damaging shape(e.g. a sphere)” around it that will trigger on overlap.
Then destroy on first overlap (or else you’ll have a railgun probably).

If you set the mass of the player to something large and the bullets to tiny then then bullets should take the change in momentum rather than the player.

Hahah maybe this will we be a gun just for GMs, thanks for your help!

Hey, it is possible I think. Set the collision groups of your pawn and your projectile to overlap each other (not block).

Make sure you have generate overlap events set to true.
Now you can react on overlaps in C++ code (maybe in blueprints, too I don’t know).
The player won’t be blocked, and you have your gravity bullet and you can “hit” the player.
What you also can do is to apply a force to damp the movement and not fully stop it when a player gets hit.

Awesome, that’s a good idea. I’ll try it out and see if I can get it working. I’ll let you know how it goes!

Interesting let me give it a try and I’ll get back to you

That did the trick! It only works if you set the “mass scaling” to anything < 1. With that value changed the character will be able to power through the collision. Thanks!

Yes UDK works slightly differently to Unity in terms of Physics & mass. In UDK you set the density (mass scaling) and it works out the mass from the collision volume. In Unity you set the mass directly. I guess UDK is slightly more fool proof because you are more likely to get correct mass, collect moment of inertia to get correct physics effects. A human should have a mass of around 100Kg & a bullet should have a mass of around .016Kg. You have to fiddle the mass scaling, which sets the mass, to achieve those masses but then you’ll get something a bit more realistic.

You can, in fact, do both those things with traces, which is generally the preferred method for fast moving, small projectiles. There’s two ways you could do it; one where you let the engine handle the simulation of the projectile’s motion and you handle the collision detection, and the other option is that you handle the motion of the projectile as well. Either way, essentially what you do is keep track of the projectile’s last position, and each tick you do a trace from the last position to the current position to see what it hit during it’s movement from last frame to this frame. You’re basically breaking up the arc of the projectile’s flight into a series of line segments, where each line segment is a raycast. The only problem is that traces (raycasts) do not account for projectile size (it’s an infinitesimally thin raycast), so if you’re projectile is large then it might be better to do a sphere-cast instead of a raycast.

Ky Mercer
You must give this a try.

I think this is a ‘must’ for projectiles that are realy fast. A faster projectile can jump over the target between two frames. Projectile with a 3000cm Velocity will travel about 100cms per frame in 30 fps. This means objects thinner then 100cm may never be get hit.