Hello, straight to my problem:
I started with the Top-Down C++ Sample and implemented simple WASD movement.
Next I made that the character always rotates to the mouse position.
The player camera is fixed on the character and does NOT rotate.
So far so good.
The problem is that I don’t want the character to actually walk to the mouse position when holding “MoveForward”.
I want him to face the mouse cursor at all times but I want him to move on an absolute plane.
In simple terms: WASD always moves UP/LEFT/DOWN/RIGHT regardless of mouse position, mouse position only changes the characters “view”(rotation).
I hope my question is understandable regardless of my broken english.
Here’s my code so far:
void ATopDownCodePlayerController::TurnToMouse()
{
//Gets local reference to the controller's character and its rotation
ACharacter *currentChar = GetCharacter();
//determine mouse Location in world space
FHitResult mouseHit = FHitResult();
bool bMouseResult = GetHitResultUnderCursor(ECC_Visibility, true, mouseHit);
//create FVector MouseHitLoc to store the impact point of the location in world below the mouse
FVector_NetQuantize MouseHitLoc = FVector_NetQuantize();
//If we hit something set the location and update the rotation
MouseHitLoc = mouseHit.ImpactPoint;
//Get Pawn Loc
FVector pPawnLoc = currentChar->GetActorLocation();
//Create new vector from player to the mouse loc
FVector TargetVector = (MouseHitLoc - pPawnLoc);
TargetVector.Normalize();
FRotator targetRotation = FRotator(0.f, TargetVector.Rotation().Yaw, 0.f);
//set character to look at mouse cursor
currentChar->SetActorRotation(targetRotation);
}
EDIT
These are my simple movement functions:
void ATopDownCodeCharacter::MoveForward(float Value)
{
if (Value != 0.0f)
{
AddMovementInput(GetActorForwardVector(), Value);
}
}
void ATopDownCodeCharacter::MoveRight(float Value)
{
if (Value != 0.0f)
{
AddMovementInput(GetActorRightVector(), Value);
}
}
I guess it’s obvious that I’m kinda new to all of this :).
Again, what my code does right now is:
- Character rotates to mouse position.
- Pressing “MoveForward” moves him to mouse position (not wanted)
Thanks for your time and help!