2D Top Down Shooting

Hey! I’m making 2D top down game (Everything is using sprites). So I have a character and I wanted him to shoot projectiles in a direction pointed by mouse click. I have no idea how can I do this. I know that I need to assign a traceline between my character and direction pointed by mouse then spawn projectile in this direction. Is there any way to make it?

You’ll need GetHitResultUnderCursor and LineTraceByChannel. I don’t know which axis we’re looking from in the picture above, but naturally that axis must be set to zero when doing the trace, because you want 2D.

I don’t recall exactly the function name but you can project the mouse in World so you get the “hit point”.
You also have something called: Convert Mouse Location to World Space

Both solution should help you.

My axis are X and Y. I did something like this and I don’t know what to do with this next. I placed those nodes in my character blueprint.

Ok, so I did something like that (Screen below). The main problem is that I want to trace in a DIRECTION clicked by mouse, not to THIS specific place. And I also have another problem. I made a print string to see which actor did i hit. When I’m clicking on the enemy, string always tells me that i hit my tilemap. I also have a few cubes on my tilemap to see how AI works along with Nav bounds. When i click on those cube, string printing correct name (cube).

Well, by converting the mouse location to a point in the world you have half the equation. The other half is to now get your actor location that you’re controlling, and use the node find lookatatrotation. Plug the vector3 of the actor location and the vector3 of the mouse hit position into it, it will give you the rotation needed to make your actor face in that direction. You can further split out the rotation structure as you only need the z rotation if you don’t care about aiming up or down vertically in a top-down shooter. Use that to set your actors rotation on Z(Yaw?) and presto, you’re dude will look where ever the mouse is located.

Using this you don’t actually need the line trace by channel. just the hit result location from get hit result under mouse cursor by channel. This plugged into tick will make it so he always follows your cursors location (as long as there’s something underneath the mouse for it to hitscan such as a floor with collision on whatever trace channel you use set to block.

Then just make it so mouse1 causes him to shoot from the forward vector of the player actor.

So I did something like this but my “bullet” flies from weird posiotion. And one more question. What if have character look like it does in a picture? Then I’ll need to set different sprites to different angles?

Change the location of spawnactor BP spell to be either your players location or get world location of something such as an arrow component parented to your player character (by default its set to hidden in game so you won’t see it ingame unless you uncheck it). You’d use get forward vector to make a rotation from it, but perhaps a simpler method for you would be to get the said arrows world rotation and plug that into the roation of your spawnactor BP spell.

Since you’re doing 2d sprites, I’d imagine you actually don’t want to rotate the actor since that rotates the player sprite to be wrong/upside down, sorry about that. Instead you can use it to set the world rotation of the mentioned arrow component of your player character as an example, as long as your sprite isn’t a child of the arrow in the hierarchy.

I tried something like this and… it worked! Thanks a lot :smiley: Now I’m thinking of changing all my graphics to TOP but I don’t know yet. If I won’t find a solution to change sprites depending on cursor location then I’ll change it.