Movement direction of actor randomly changes when I change the ActorRotation

Hi!
I am facing problems with Moving and rotating actor, I created a simple waypoint system (without using NavMesh) and I used the following:

CDistance = FVector::Dist(Waypoints[WP]->GetActorLocation(), GetActorLocation());
    if (CDistance <= 150.f) //CDistance is distance to waypoint calculated in Tick()
    {
        WP++;
        if (WP == Waypoints.Num())
            WP = 0;
        CalculateNextDirection(CurrDirection);
    }
    else
    {
        UE_LOG(LogTemp, Warning, TEXT("Should Move"));
        AddActorLocalOffset(GetActorForwardVector());
    }

//Calculate Next Direction Method
CalculateNextDirection(FVector& CurrDirection)
{
    UE_LOG(LogTemp, Warning, TEXT("Calculating next waypoint"));

    CurrDirection = Waypoints[WP]->GetActorLocation() - GetActorLocation();
    CurrDirection.Normalize();
    FRotator NewRotation = FRotationMatrix::MakeFromX(CurrDirection).Rotator();
    NewRotation.Pitch = 0.f;
    NewRotation.Roll = 0.f;

    SetActorRotation(NewRotation);

}

It works but after reaching the waypoint when Actor Rotates towards another waypoint, its movement direction changes (It starts to move in some random direction). What could be the problem?

Where are you calling this? Also please use a code block in the future when posting code.

Indent code by 4 spaces or surround it with 3 backticks

That code is written under Tick() Function…

Oh I see it now. You’re mixing world and local space.

AddActorLocalOffset(GetActorForwardVector());

This should either be

AddActorWorldOffset(GetActorForwardVector());

or

AddActorLocalOffset(FVector::ForwardVector);

FVector::ForwardVector is just FVector(1,0,0)

Oh! Ok, thanks for clearing out about this!

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

Privacy & Terms