Getting Projectiles to Stick to Terrain and Player


By Will Fox

3/10/2023

Hey everyone!

This blog post will be relatively short this week as most of the issues I ran into have been debugging-related. So, one of the more critical issues I figured out was getting projectiles - specifically arrows - to stick to the terrain and become a child to the player mesh if it hits them. I originally started out by figuring out how to get terrain collision working. The original arrow projectile could hit the player, but it would phase through terrain if it missed. To solve this, I found a tutorial on YouTube that walked me through getting terrain collision working. The main thing was using the "On Collision Hit" node and also changing some of the specific check boxes under the collision drop-down. For some reason, the "On Collision Hit" node only activates under some very specific conditions - which I am still confused about - so I ended up just making brand new projectiles since simply changing the "collision event" node didn't work.

Next, I needed to spawn a static mesh directly where the arrow hit the terrain. This was pretty easy, as all I needed to do was create an actor that held a static mesh component. Then, whenever the "On Collision Hit" node was executed, I would spawn an actor at the hit location that used the rotation and world vectors of the arrow projectile. I would then set the death timer of the arrow to around 10 seconds and then delete the actual arrow projectile so it couldn't hurt the player twice - and also since it didn't need to exist anymore. This was pretty much the entire implementation for getting the arrow projectile to stick to the terrain. The tricky part was getting arrows to stick to a dynamic object - like the player that moves around.

The way that I was able to get this to work was very similar to the terrain sticking. However, the nodes I needed to use were a little different. The node that needed to change was "spawn actor from class." Instead, I used a node called "Add Child Actor Component," which adds a child component to a target actor. So, to get the implementation working for this node, I just needed to add some logic to check if the arrow hit the terrain or the player. If it hit the player, it would add a child actor to the player - with about a 5-second death timer. But then, I found a pretty significant issue; since we're dealing with child actors now, we're dealing with relative space instead of world space. This meant that I needed to convert the child actors' location to relative space. Again, though, Unreal has a pin inside of the "On Collision Hit" node that returns the transform of the component that got hit. I just needed to convert the hit transform from world space to relative space. Luckily, Unreal has nodes built just for this called "inverse." You get relative space by getting the inverse of the location and rotation.

Now, the arrow projectile sticks to both the player and the terrain. The arrow doesn't stick exactly where it's supposed to with the player, but, as of now, it gives the intended effect that we want.

Leave a comment

Log in with itch.io to leave a comment.