Download

Target lock movement drifts

Taking the ThirdPersonneCharacter c++ class as an example, I tried to have my character running around a locked target. It almost work, as the model is facing the target and the correct animations are played when strafing. Sadly the distance between the character and the target increases as the character turns around the target. I can’t find why.
Here is a video where you can see the result: [video]https://youtu.be/K0zgYWhbhfA[/video]

Here is the code I wrote:
(MoveRight is binded to the “MoveRight” input winth the BindAxis function)



void AKnightFightCharacter::Tick(float DeltaTime)
{
	FRotator NewRot = FRotationMatrix::MakeFromX(lockTarget->GetActorLocation() - this->GetActorLocation()).Rotator();
	NewRot.Pitch = 0;
	lookAtDirection = NewRot.Vector();

	SetActorRotation(NewRot);
	doMoveRight();
}

void AKnightFightCharacter::doMoveRight()
{
	if (moveRightValue != 0)
	{
		// find out which way is right
		const FRotator Rotation = GetActorForwardVector().Rotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);

		// get right vector 
		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
		// add movement in that direction
		AddMovementInput(Direction, moveRightValue);
		moveRightValue = 0;
	}
}

void AKnightFightCharacter::MoveRight(float Value)
{
	moveRightValue = Value;
	if (animationInstance)
		animationInstance->direction = Value * 100;
}


I first though the the rotation in Tick was calculated after the call of MoveRight, which could have caused the drift. That’s why I wrote the doMoveRight function, but it doesn’t help.