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?
Announcement
Collapse
No announcement yet.
2D Top Down Shooting
Collapse
X
-
Originally posted by FL1PPY19 View PostHey! 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?
-
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).
Comment
-
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.Indie Dev - Whiskey Cat Softworks
Currently developing a colony survival game!
https://twitter.com/WSoftworks https://www.facebook.com/WhiskeyCatSoftworks/
Comment
-
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.Indie Dev - Whiskey Cat Softworks
Currently developing a colony survival game!
https://twitter.com/WSoftworks https://www.facebook.com/WhiskeyCatSoftworks/
Comment
Comment