Can't get actor to face direction properly

Hello, total noob here!

I’m trying to get my actor to instantly face the direction of the vector of witch it’s moving.

But i cant seem to instantly rotate the actor the way i want to. Instead of just facing the right direction, it slowly rotates in the direction i want.

Video of the problem here

My code snippet looks like this:

	FQuat quat = GetActorLocation().ToOrientationQuat();
	PlayerActorComponent->SetWorldRotation(quat);

If anyone knowes how to solve this, it would be great.

Well solution depends on how do you move your character. If you have your input vector e.g. you have pressed W meaning your movement vector is FVector(0.0f, 1.0f). Then you need to normalize the vector (in case you have both W and D pressed you would get vector that is not normalized) by callinng .Normalize() function on your input vector. Then you call .Rotation() function on your normalized vector which returns FRotator stucture. The simply set your APawn rotation by calling YourPawn->SetActorRotation(NewRotation).

I’ve already answered your question, waiting for the moderator approval.

CurrentVelocity???

Is it an actor, pawn or character? Are you using some movement component?

It’s a pawn (I said actor cause i mixed them up) the component is a UStaticMeshComponent

  • So, you’re not using any movement component?
  • How are you moving the pawn? With AddActorLocalOffset?
  • Where did you put the code above?
  • Can you move diagonally?

It would really be faster if you show us the pawn code.

PlayerComponent = CreateDefaultSubobject<UStaticMeshComponent(TEXT("PlayerComponent "));

PlayerComponent ->SetupAttachment(RootComponent);

.
.
.
void APlayerPawn::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	FQuat quat = GetActorLocation().ToOrientationQuat();
	//UE_LOG(LogTemp, Warning, TEXT("MyCharacter's Location is %f"), rotation);
	OurVisibleComponent->SetWorldRotation(quat);

	{
		if (!CurrentVelocity.IsZero())
		{
			FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
			SetActorLocation(NewLocation);
		}
	}
}

That’s pretty much the code i use.

In .h file:
FVector CurrentVelocity;
in .cpp file:
CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f) * fDash;
or

	CurrentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f) * fDash;

fDash is a float triggered by user input that determens player speed

Remove the quat line and try this:

 //FQuat quat = GetActorLocation().ToOrientationQuat();
 //PawnMesh->SetWorldRotation(quat);

if (!CurrentVelocity.IsZero())
{
	FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
	SetActorLocation(NewLocation);
	PawnMesh->SetWorldRotation(CurrentVelocity.ToOrientationRotator());
}

It will instantly rotate the component towards the CurrentVelocity’s direction.