Character Rotate to Cursor Location

Evening all. I’ve spent some time trying to get this working, to no avail.

I simply want the character to rotate towards the cursor location on screen. In a top down perspective.

I have it working like this.


But this isn’t ideal as it is tracing for objects under the cursor, as opposed to using the actual screen location of the cursor.

As you can see in this video, with my mouse held still, the character snaps to different rotations when i move under an object and back out again.
[video]- YouTube

So. I’m trying to do something like this…


In theory this would work a lot better. But I just can’t seen to get it working in any usable way.

I tried printing the return values of the nodes like this


But the values from the hit result under cursor and the Get Mouse Position are completely different.

Any one have any ideas how to go about getting this working correctly?

Thanks in advance! Much appreciated!

There’s many ways to do it.

  1. Go to your Project Setting –> Engine –> Collision and add a new Object Channel
  2. When you Get Hit Result Under Cursor by Object, drag a pin from the Object Types, make an array and set the above-mentioned Object Channel there
  3. Ensure that your floor mesh is using collision Custom Preset and Collision Type is set the Object Channel you created

object channel.png

Another way to do it is to project a plane and trace for intersections (Plane Origin is your floor):

Thanks for the response.

Unfortunately, neither of those are really ideal.
The first one, Every single object used as floor would need to be tagged. What about when the cursor is over a wall, or an object? The player will just face 0,0,0.

And the second one isn’t ideal as the game will be 3D. You will be able to move up and down all the time.

The only way to make it work is to have the player face the position of the cursor on screen. As opposed to any objects in the scene. That just doesn’t make sense to me. It just doesn’t work either.

I need to get the mouse location in screen space and get some kind of usable information from that to rotate the character.

Cheers!

ConvertMouseLocationToWorldSpace does exactly that. Mouse position in 3d space. In this case, Line Trace should do the trick:

Cheers for the response Everynone. Unfortunately that setup has the same issue as in the video above.

See here

Thanks again!

Could you not break the World Location and World Direction from ConvertMouseLocationToWorldSpace, split the Start & End on the LineTraceByChannel, then only drag in the X & Y values?

I did exactly that.


It still jerks the character around when your cursor moves over any objects.

As you can see here.

Thanks again all!

If you really want a quick and dirty solution, use the line intersection method. Instead of using floor’s position as the Plane Origin, use your character’s position instead. Anyway, whether the player goes up and down does not matter, get the coords that you need.

To be honest, you will be limiting yourself if you take the easy way here. I still suggest you use the object channels. It’s a tad more work but you will be able to obtain more information about your world this way, especially at a later stage. And no, you do not need to ‘tag’ everything - not by hand at least. If your levels are going to me made out of modular pieces, you can have a class with the desired settings and instantiate, in the editor or during run-time, swapping meshes and materials as needed.

I think your suggestion of using the character position or floor position might be a good solution.

I’m not trying to take the easy way or the dirty way. I’m just looking for a solution that works well.

I can see what you mean about the object channels. Seems like more work than is needed. Not sure what more information I would need about the world.

Still trying to get this working better.

I don’t suppose you could go into more detail. I think I understand what you are suggesting. But I’m not sure how to go about setting it up.

Based on your previous advice. This is what I have set up so far.
Imgur
It sort of works. The camera doesn’t jolt if the cursor moves over an object. But the character doesn’t seem to face where the cursor is located.
Scratch that. I was being dim. I used the character location as the input for the “Find Look at Rotation” “Start”. And it seems to be working brilliantly.

Thanks again.

2 Likes

Glad to hear that!

Thanks for the pointers. I really appreciate it. Sorry for being a dumbass! haha

Thanks again! Very much appreciated.

Old thread, but just wanted to share it helped me but I can suggest a further improvement.

When creating the upright plane, set the plane’s origin to the location of your weapon. This way the line projected to the mouse position will be from the barrel of the gun and shots will go perfectly where the cursor is.

If you use the character’s location the shots will always go above your cursor.

2 Likes