[Resolved] Top-Down: Click -> Face enemy -> Shoot projectile.

Hi everyone.

Been trying this last week to get get this simple function to work but I must be missing something obvious.
Making a small topdown rpg, but having issues with projectile and combat.

I use the standard left mouse click/hold to move and have a projectile fireball on right mouse button.
As for now I have to steer my character towards the enemy before shooting. But there must be a way to have face enemy, and fire all in that one right click?
I’ve tried making a blueprint to always face the mouse pointer, but I would prefere to auto face pointer and fire instead of the character always following the mouse pointer.

Any suggestions would be much appreciated.

Thanks!

FindLookAtRotation function should do the job. Just pin in you character location and enemy location (or location pointed by mouse) and it will return rotation your character needs to be to face that enemy (or pointed location). Break returned Rotator and use its Z(Yaw) to make new Rotator, it will prevent rotating character about axes other than Z. Use new Rotator to SetRotation of your character. You can also break character current rotation and pin its X and Y into that new Rotator so your character will keep its X and Y (if they are different than 0).

Thanks! I will try this when I’m off work.
I tried with FindLookAtRotation last night but my character and projectile was offset by 90’ degrees and I could’n get it right.
I see now that you are using two Break Rotators, I only thought of using one…

Thanks!

I tried something like this last night but not with two Break rotators. Will give it a go tonight after work! :slight_smile:

Be very mindful when breaking and re-making rotators. Gimball lock is a beast to try and troubleshoot as it’ll work sometimes, and other times produce erratic results. I’d personally recommend always using “Combine Rotators” to save yourself from the headache and frustration. You should be able to get the look at rotation, get the difference between that and the current rotation, and then add rotation to the actor. That would totally remove the need for breaking/making rotators and risking running into gimball lock.

That in consideration, I wouldn’t imagine this example would run the risk of experiencing gimball lock (it looks great to me and should work well, glad he/she posted it). The above comment is just a sidenote so you don’t go down the same path many of us have in troubleshooting code that looks like it should work perfectly, but makes you want to throw your monitor through the window. You’ll usually run into gimball lock when you are combining multiple values in a single axis, not when using three perfectly acceptable axis values.

Thanks for the input/tip!

I’ll try things out and see where I land on this.

My porch doesn’t have room for more monitors and my window replacement budget is allready blown :slight_smile:

OK.

So I got the direction of my character right this time. But it still always rotates towards my mouse.
Looks so silly that my character is spinning around when not in combat. E.g. inventory, etc etc.

My goal is to only rotate when right mouse button is pressed (Auto face enemy and fire), or only rotate when holding down shift so I have a “combat toggle” of sorts?

BP attached

Thanks!

Create a boolean variable, something like “isCombatMode”. Default it to false. Then, on pressing Shift or clicking, set it to true, on release set it to false. If you do both, make sure you account for both, so you don’t have someone holding shift AND clicking, then releasing one and toggling combat mode off. Finally, set the functionality you posted a screenshot of to only run when isCombatMode is true.

Awesome!

Works like a charm now!

A big thank you to both!