Problem making turret to rotate back to default if tank is out of firerange

I was trying to rotate turret back to default rotation by using GetActorForwardVector(), but it is giving me fixed location in the world map, unlike in Crypt Raider. Why is this and how can I fix?

this is also happening to Tank

    Super::Tick(DeltaTime);

    DrawDebugLine(GetWorld(), GetActorLocation(), DefaultRotation, FColor::Red, false, 1.f);
    DrawDebugSphere(GetWorld(), GetActorLocation(), 100.f, 13, FColor::Cyan, false, 1.f);
    DrawDebugSphere(GetWorld(), GetActorForwardVector(), 100.f, 12, FColor::Blue, false, 1.f);
    
    // Find the distance to the Tank
    if (Tank)
    {
        float Distance = FVector::Dist(GetActorLocation(), Tank->GetActorLocation());
        // Check to see if the Tank is in range
        if(Distance < FireRange)
        {
            // If in range, rotate turret toward Tank
            RotateTurret(Tank->GetActorLocation());
        }
        else
        {
            RotateTurret(GetActorForwardVector());
        }     
    }
    

GetActorForwardVector() isn’t a location yet you’re trying to use it as such.

In CryptRaider you were using the forward vector along and an amount to scale the forward vector by; which would mean you end up with a vector going in the same direction as the forward vector that is the same length that you scaled the vector by.

Adding this to the current location will then give you a location that is X amount of units in that direction.

Example in 2D with the direction being right (Rotation.Vector())
Reach being 5
and the current location being (3, 3)

1 Like

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

Privacy & Terms