I’m making grid movement. I’m using timers to move the character from one tile to another, each frame I set its actor location to a interpolated value from his originalPosition to the target one.
However, with the spring arm component, I’ve noticed some jitter or undesired camera movement when I hold down the key.
I’ve disabled Orient Rotation to Movement and everything in between, yet I still have this.
Please take a look at this video for reference :
Here’s the code that moves the character.
void AGridManager::RequestMove(ACharacter* _caller, FVector2D _input, float _movementSpeed)
{
// Save In-unit
unit = _caller;
// Save In-input
input = _input;
// Desired Cell Location
FVector2D targetCell = playerCurrentCell + _input;
// Desired World Location
FVector TargetPosition = CellToWorld(targetCell);
TargetPosition.Z = unit->GetActorLocation().Z;
// Calculate the movement direction
FVector Direction = (TargetPosition - unit->GetActorLocation()).GetSafeNormal();
// Calculate distance and time required for movement
float Distance = FVector::Distance(unit->GetActorLocation(), TargetPosition);
float MovementSpeed = _movementSpeed;
float TravelTime = Distance / MovementSpeed;
// Start a timer to handle movement
unit->GetWorldTimerManager().SetTimer(MovementTimerHandle, [this, TargetPosition, MovementSpeed]()
{
float ElapsedTime = GetWorld()->GetDeltaSeconds();
FVector NewLocation = FMath::VInterpConstantTo(unit->GetActorLocation(), TargetPosition, ElapsedTime, MovementSpeed);
unit->SetActorLocation(NewLocation);
if (FVector::Distance(unit->GetActorLocation(), TargetPosition) < KINDA_SMALL_NUMBER)
{
unit->GetWorldTimerManager().ClearTimer(MovementTimerHandle);
unit->SetActorLocation(TargetPosition);
OnMovementEnded();
}
}, unit->GetWorld()->GetDeltaSeconds(), true);
}
The reason I use SetActorLocation() and not the pre-built AddMovementInput() from the CharacterMovementComponent is because I need this function to be called on any actor from the game. It helps with not repeating code and keeping everything in check.
Thank you in advance !