Hello all, I’m trying to replicate Diablo style movement in my game over multiplayer which is giving me a little bit of trouble at the moment. I was able to get the movement to work locally, but now as I try to get replication working I have run into some trouble. My goal is when the player releases the mouse button, the character will walk towards that point. However, when the player holds down the mouse button or presses the W key, the character will continually walk in the direction of the mouse. I have gotten the walk to destination when the player releases the mouse button working however the continually move towards the mouse is not working.
For the point and click movement, I followed this guide. I’m trying to add the move-forward movement like this:
void AMyPlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
// ...
InputComponent->BindAxis("MoveForward", this, &AMyPlayerController::OnMoveForward);
}
void AMyPlayerController::OnMoveForward(float Value)
{
FVector MouseLocation = GetWorldMouseLocation();
// Stop move to location
if (Value >= 1.f)
{
MoveForward(MouseLocation);
}
}
void AMyPlayerController::MoveForward(const FVector MouseLocation)
{
ServerMoveForward(MouseLocation);
}
bool AMyPlayerController::ServerMoveForward_Validate(const FVector MouseLocation)
{
return true;
}
void AMyPlayerController::ServerMoveForward_Implementation(const FVector MouseLocation)
{
StopMovement();
// Move towards the mouse
GetPawn()->SetActorRotation(MouseLocation.Rotation());
GetPawn()->AddMovementInput(MouseLocation, 1.f);
}
And
FVector AMyPlayerController::GetWorldMouseLocation() {
int32 ViewportWidth, ViewportHeight;
float MouseX, MouseY;
GetViewportSize(ViewportWidth, ViewportHeight);
// UE returns the mouse position relative to the center of the screen
// To be able to calculate the mouse position from the top left, we must
// substract the mouse position from half the size of the viewport
ViewportWidth = floor(ViewportWidth / 2.f);
ViewportHeight = floor(ViewportHeight / 2.f);
GetMousePosition(MouseX, MouseY);
// Calculate the actual mouse position into a vector
FVector MousePositionVector(ViewportWidth - MouseX, ViewportHeight - MouseY, 0.f);
// Don't remember why we have to do this...maybe for projection?
MousePositionVector.Normalize(0.f);
// Flip the X coordinate, to get to the top
MousePositionVector *= FVector(1.f, 1.f, 0.f);
// Rotate the vector to account for the camera rotation
MousePositionVector = MousePositionVector.RotateAngleAxis(-45.f, FVector(0.f, 0.f, 1.f));
return MousePositionVector;
}
Basically the problem right now is that nothing happens when I trigger the MoveForward axis event