Jitter movement from the camera while using SetActorLocation()

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 !

You might have some luck using ‘teleport’ rather than ‘set actor location’, as it understand when to move the actor during the frame.

If you’re refering to TeleportTo(), it somehow made the jitter far worse…

Here’s the function that rotate the player when a new input is pressed:

	// Save In-unit
	unit = _caller;
	// Save In-input
	input = _input;

	float ElapsedTime = 0.0f;

	// Save the initial rotation
	FRotator InitialRotation = unit->GetActorRotation();

	// Convert the 2D input direction to a 3D direction
	FVector WorldInput = FVector(_input.X, _input.Y, 0.0f).GetSafeNormal();

	// Calculate the rotation angle around the Z-axis
	float TargetYaw = FMath::Atan2(WorldInput.Y, WorldInput.X) * 180.0f / PI;

	// Create the target quaternion based on the calculated angle.
	FRotator TargetRotation = FRotator(0.0f, TargetYaw, 0.0f);

	// Start a timer to handle the rotation
	unit->GetWorldTimerManager().SetTimer(RotationTimerHandle, [this, ElapsedTime, InitialRotation, TargetRotation, _rotationTime]() mutable
		{
			// Increment ElapsedTime using DeltaSeconds
			ElapsedTime += unit->GetWorld()->GetDeltaSeconds() * _rotationTime;

			// Slerp between initial and target quaternions
			FQuat NewQuat = FQuat::Slerp(FQuat(InitialRotation), FQuat(TargetRotation), ElapsedTime);
			FRotator NewRotation = NewQuat.Rotator();

			// Set the new rotation on the character
			unit->SetActorRotation(NewRotation);
			// Check if the target rotation is reached
			if (ElapsedTime >= 1.0f)
			{
				// Stop the timer when the target rotation is reached
				unit->GetWorldTimerManager().ClearTimer(RotationTimerHandle);
				unit->SetActorRotation(TargetRotation);
			}
			
		}, unit->GetWorld()->GetDeltaSeconds(), true);

If it helps or not.
This situation is complex since I can’t tell if the problem comes from the player movement or the camera.
Even with moving from one tile to another and coming back to the original tile, the camera shakes like crazy.

1 Like

The Camera lag is the problem. When it’s turned off, everything is super smooth.

Kinda wish I had the ability to keep it.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.