Hi everyone, I have a little issue with my main character, I have set up a movement mechanic where if you click on a object, it will move to it or interact with it depending on the type of actor it is, based on these, I store a FVector of the mouse click position and change it to PlayerController->GetHitResultUnderCursorByChannel(), which returns the hit.position and the hit.actor, after that, I use the following code in the Tick function to move the character to that position and change it’s looking rotation to it:
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
PlayerInput.Sanitize();
// First look in the direction the Player want's to move to
FVector DesiredMovementDirection = PlayerInput.GetMovementLocation();
if (!DesiredMovementDirection.IsNearlyZero() && DesiredMovementDirection != FVector::ZeroVector)
{
//Target aquired, time to attack it
FRotator DirectionToTarget = (DesiredMovementDirection - PlayerDirection->GetComponentLocation()).GetSafeNormal().Rotation();
PlayerDirection->SetWorldRotation(FRotator(0.0f, DirectionToTarget.Yaw, 0.0f));
FVector MovementDirection = PlayerDirection->GetForwardVector();
FVector Pos = GetActorLocation();
Pos.X += MovementDirection.X * (PlayerInput.IsDoubleClicked() ? PlayerMovementSpeed * 1.45f : PlayerMovementSpeed) * DeltaTime;
Pos.Y += MovementDirection.Y * (PlayerInput.IsDoubleClicked() ? PlayerMovementSpeed * 1.45f : PlayerMovementSpeed) * DeltaTime;
SetActorLocation(Pos);
Pos = GetActorLocation();
if (FVector::PointsAreNear(Pos, DesiredMovementDirection, 1.5f))
{
PlayerInput.MoveTo(FVector::ZeroVector);
}
}
}
The issue is that once the character has arrived into the location clicked on the screen which is saved inside PlayerInput.GetMovementLocation(), I did a small verification to see if the current location was equal or nearly equal to the PlayerInput.GetMovementLocation() if that was true, it was supposed to set the value in PlayerInput.GetMovementLocation() to FVector::ZeroVector, but it never goes inside that validation, I did it in multiple ways, but I couldn’t get it to work correctly, so I want to ask you guys to see if you have any right ideas on how to verify if the character is right on top or near the clicked location that is stored in PlayerInput.GetMovementLocation()
Here is a Gif showing one of the issues I found with this, If I don’t have the UE4 Editor windows active at the moment, the character moves quite weird even though it has already reached it’s destination.
[GIF Link][1]
The other Issue occurs for the same reason has I have described before, the character reaches it’s position but since it doesn’t enter the if to clear the movement location, the player starts dancing in the same position constantly.
Thank you, and I hope that you guys have some ideas on how to solve this issue.