Why is my Character Facing Mouse direction But Losing Orientation when using TopDown?

Hi guys, I’m attempting to create a top down character that moves in the cardinal directions with WSAD and faces towards the mouse. So far I have this working most of the time. However, when I move the character eventually it loses its orientation and can no longer face towards the mouse. During play I seem to be able to “fix” it by aiming my characters movements towards where the mouse is facing. After a bit it’ll reorient itself and work again. I’m figuring this must be caused by either the character’s movement or the camera following it. Anyone have any ideas here? I’m spent hours exhausting all mine.

Also here’s my mouse look at blueprint

Do you know how convert mouse location to world space works? I have the same input system as you do, and basically decided that it didn’t do what I wanted and ended up just utilizing vector math to get the mouse location on the XY plane.

I can post the blueprint when I get home.

EDIT:

Alright. This is the blueprint.

Not sure if there’s a better way to do it, but it works for me. You can just copy it if you want but I’ll try explain for anyone that doesn’t understand.

The “set rotation” section is pretty straightforward. Just get the pawn location, the mouse target, and update the yaw of the pawn to face the target.

To actually get the mouse target, a little math needs to be done.
What I wanted for my game was for the mouse to be projected from the camera onto a horizontal plane the same level as the floor.
That way, if the player has the mouse on top of a tall object, the player will be facing the point on the floor behind the tall object rather than snap to face the bottom of the object. (I thought this was annoying in the top-down starter project)

We want to find the point of intersection between our floor plane and the line that goes from the camera position, through the mouse location in world space.

The plane for me is where Z = 0. You’ll probably have to do some more work that I haven’t done here if you have a slanted floor but hopefully your game isn’t as crazy as that.

We can make our line from two points; the location of the camera P1 and the location of the mouse in world space P2

2469-mouseposrotation2.png

We want x and y values for our mouse target, and we have a z value

We can use the parametric x,y,z equations for a line to get those.

x=P1x+dx*t

y=P1y+dy*t

z=P1z+dz*t

where dx,dy,dz is the difference vector from P1 to P2 and t is how far along the line from P1 to P2 the point is that the x,y,z values make.

We can easily work out the value of t where the line intersects our plane by setting z = 0.

Rearrange the z equation: t = (z - P1z) / dz.

z = 0 on our plane so t = - P1z / dz.

The difference vector is just P1 - P2, which there is a blueprint node for.

Then we can plug in t to the first two equations to find x and y, and that’s our mouse point.

You can replace 0 with another value, or use your player’s Z position instead so that it works on different elevations :smiley:

Let me know if this helps you, or if there’s a better way to do it. I want to add the best method to the wiki.

Yeah, it’d be much appreciated if you can post the blueprint.

I got the solution converting mouse location to word space from another answer here.

Updated my answer, hope it helps.

That worked perfectly! Thank you very much!

This doesn’t work for me, any ideas? I have the top-down template, mouse enabled on new Controller, changed movement to WASD.

I put a print string on the last “Make Rot”, right before the Set Actor Rotation and it printed constant Pitch and Rolls, Yaw was either “179.xxxx” or “-179.xxx”. Now for some reason it stopped printing to my screen, so I can’t screenshot it. Thanks.

Just a second, the string stopped printing to the screen, found a solution, hit Compile and then the Editor crashed, already sent a bug report to Epic. Thanks for answering again.

Managed to do it, and it isn’t correct apparently. It’s getting something around X=-620.823 Y=-57.652 Z=0.000 (LogBlueprintUserMessages: X=-620.823 Y=-57.652 Z=0.000LogBlueprintUserMessages - Pastebin.com)

Is it running? Can you print the output vector and see what you’re getting?

Is it getting the right vector from the “Get target” section? Try printing or using “Debug draw point” to find out. (set it to white, 0.5 second duration)
It’s worth noting I made mine from empty project rather than top-down, maybe there’s something that messes with the rotation…

Managed to do it mixing your code with OP’s. Works for now.

I’ve tried all of these but still have an orientation issue when moving - I’m using WASD on a top down game. You can see a gif of my problem in the link on my question page:

any ideas to fix this?