Download

How to smoothly rotate a character?

Hello,

i’m trying to smoothly rotate my patrol, i have already tried InterpSpeed 0.1, 0.0001, 5, 6, 12… but seems i can’t solve the issue. The code i’m using to smoothly rotates my patrol it’s within the method Tick() as you can see in the code.

What am I doing wrong? Thank you.



void APatrolController::Tick(float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);

	if (Patrol)
	{
			uint8 Evaluate = this->ActualState();
			FRotator Obtained;

			if (Evaluate == PatrollingState)
			{
				FVector pVector = Patrol->GetPathIndex(ActualRoute);

				Obtained = GetPatrolRotator(Patrol->GetPathIndex(ActualRoute));
			}
			else if (Evaluate == PursueState)
			{
				if (Player)
				{
					Obtained = GetPatrolRotator(Player->GetActorLocation());
				}
				else
				{
					this->FindPlayer();
				}
			}
			else if (Evaluate == ReturningState)
			{
				Obtained = GetPatrolRotator(LastPosition);
			}

			FRotator newRotator = FMath::RInterpConstantTo(Patrol->GetActorRotation(), Obtained, DeltaSeconds, 12.0f);

			Patrol->SetActorRotation(newRotator);
		
	}
}

FRotator APatrolController::GetPatrolRotator(FVector pVector)
{
	if (Patrol)
	{
		FRotator ReceivedRotator = FRotationMatrix::MakeFromX(pVector - Patrol->GetActorLocation()).Rotator();

		FRotator ToSend(0, ReceivedRotator.Yaw, 0);

		return ToSend;
	}

	return FRotator(0, 0, 0);
}

void APatrolController::FindPlayer()
{
	for (FConstPawnIterator It = this->GetWorld()->GetPawnIterator(); It; ++It)
	{
		APrototipoCodeCharacter *TestPawn = Cast<APrototipoCodeCharacter>(*It);

		if (TestPawn)
		{
			Player = TestPawn;
			break;
		}
	}
}



https://www.youtube.com/watch?v=ol-ZeMQdBVM&feature=youtu.be

just to give you guys a look on whats happening =)

Solved.

*A couple of Unreal devs in that thread seem to say it can be done like this:

“To fix this, go into the movement component of the character and turn on “Orient Rotation to Movement”, then go into the Defaults of the character and turn off “Use Controller Rotation Yaw”. Let me know if that helps.”*

That sure comes in handy, thanks mate!