Character not rotating towards mouse when moving

Hey, i’m working on a small project where the player controls a character in a top down view. The player can use wasd to move the character and use the mouse to make the character rotate. I’m currently using the third person template although i have rewritten some things.

I have managed to get so the character can rotate towards the mouse, but this only works if the character is standing still. The reason i’m using planes and line intersection is because i need to have the character able to rotate towards the mouse when the character is standing on something that is tilted.

// Create plane with actors up vector and players location
FVector ActorUp = GetActorUpVector();
FPlane RotationPlane = FPlane(GetActorLocation(), ActorUp);

//DrawDebugSolidPlane(GetWorld(), RotationPlane, GetActorLocation(), 250.0f, FColor::Red);

// Get mouse location in the world
FVector MouseDir;
FVector MouseWorldPosition;
PlayerController->DeprojectMousePositionToWorld(MouseWorldPosition, MouseDir);

// Create a point under the mouse world position to use when creating the line
FVector MouseWorldPosDown;
MouseWorldPosDown = MouseWorldPosition - (ActorUp * 100.0f);

// Intersection point between the line and plane
FVector IntersectPoint;
IntersectPoint = FMath::LinePlaneIntersection(MouseWorldPosition, MouseWorldPosDown, RotationPlane);

// Find the direction to the projected point from the player
FVector IntersectDirection = IntersectPoint - GetActorLocation();
IntersectDirection.Normalize();

// Draw line in the direction to look
FVector Temp = GetActorLocation() + (IntersectDirection * 1000.0f);
DrawDebugLine(GetWorld(), GetActorLocation(), Temp, FColor::Magenta);

// Set the actor rotation
SetActorRotation(IntersectDirection.Rotation());

This works fine when the character is standing still but if i start moving the character it stops working and start to look in weird directions. Since i’m using the third person template with the animation starter pack might this cause issues for the rotation?

However if i use this piece of code instead of the other set rotation it seems to work just fine. Why is this?

SetActorRotation(FRotator(0.0f, MouseDir.Rotation().Yaw, 0.0f));

Continued testing a bit more, turned off the animations and it still behaves in the same way. Is there a way to stop the character so that it wont rotate in the direction the character is moving perhaps?

In the character movement component, search for the Orient to controller or movement (dont have the engine or code at hand), there are two sources that influence the behavior. Look for the docs and other questions here in the hub:

I found what you were talking about, i turned off “Orient rotation to Movement” which stopped the character from rotating in the direction it is moving but i still had the issue that it was not rotating towards the mouse.

This is a screen shot of what is happening. Currently in the screen shot the player is moving to the west and the mouse is currently (although you can’t see it) to the north west-ish. As you can see both the pink line which indicates which way the character should rotate and the character which is rotated to the pink line is in the wrong direction. But it is not always in the opposite direction.


So it seems something goes wrong in the code that calculates the direction when the character starts to move.

Well if you want to move through the mouse, i would do the following: 1. use orient to movement. 2. project the mouse position to the plane you are moving on 3. then set the movement vector to move towards the projected position (do not rotate manually)

I could do that but i don’t think it would fit the game that i’m creating.

if you want to rotate faster use the rotation rate. as far as i know this is the recommeded way to rotate a pawn. also, you may reuse that for AI movement.

When using rotation rate i don’t set the rotation directly but instead have the pawn rotate to a certain point over time, right?
Even if i switched to this, i would still need to get the correct direction to the mouse when the player is moving. Which is currently not working.

You have to use AddMovementInput(Direction, Value);
where the direction is the vector form your char to the deprojected mouse position. Value is in you case most likely 1.0f. See the documentation for more on this.