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));