My turret and barrel rotations are all inverted

Hey guys,

I’ve been having this problem for a while and I cannot get it fixed! As you can see in the image, the line traces are drawing from the StartLocation to the HitLocation, but when all the rotations are applied, the turret and barrel both rotate in the inverted rotations. You can see the tank aiming at the mountain’s barrel is facing downward and the turret is backward where the barrel should actually face upwards (far distance) and the turret should be facing the opposite directions. I’ve gone as far as copy-pasting all of Ben’s code from his .CPP and .H files, but this still happens. Please, I’ve been stuck on this for days, halted at this lecture. The barrel has been inverted ever since I start coding it.

Barrel:

void UTankBarrel::Elevate(float RelativeSpeed)
{
    // Move the barrel the right amount this frame
    // Given a max elevation speed, and the frame time
    RelativeSpeed = FMath::Clamp<float>(RelativeSpeed, -1, +1);
    auto ElevationChange = RelativeSpeed * MaxDegreesPerSecond * GetWorld()->DeltaTimeSeconds;
    auto RawNewElevation = RelativeRotation.Pitch + ElevationChange;
    auto Elevation = FMath::Clamp<float>(RawNewElevation, MinElevation, MaxElevation);
    SetRelativeRotation(FRotator(Elevation, 0, 0));
}

Turret:

void UTankTurret::Rotate(float RelativeSpeed)
{
    RelativeSpeed = FMath::Clamp<float>(RelativeSpeed, -1, +1);
    auto RotationChange = RelativeSpeed * MaxDegreesPerSecond * GetWorld()->DeltaTimeSeconds;
    auto Rotation = RelativeRotation.Yaw + RotationChange;
    SetRelativeRotation(FRotator(0, Rotation, 0));
}

MoveBarrelTowards:

void UTankAimingComponent::MoveBarrelTowards(FVector AimDirection)
{
    auto BarrelRotator = Barrel->GetForwardVector().Rotation();
    auto AimAsRotator = AimDirection.Rotation();
    auto DeltaRotator = AimAsRotator - BarrelRotator;
    Barrel->Elevate(DeltaRotator.Pitch);

    if (FMath::Abs(DeltaRotator.Yaw) < 180) {
	    Turret->Rotate(DeltaRotator.Yaw);
    }
    else {
	    Turret->Rotate(-DeltaRotator.Yaw);
    }
}

Any workarounds appreciated!

For those who MAY have this problem, here is a fix that made it work for me:

So, after lots and lots of trial and error, and hours upon hours of work, I have finally found the root of the problem. And this will be a mistake I’ll never make again. Basically the Forward Vectors of my tank barrel and turret were facing the opposite directions! Which was a mistake made in the model creation. All I had to do was go into my modelling program (C4D) and rotate the turret and barrel models 180 degrees (YAW).

I’m posting this solution for those who may have this problem! Hope this helps!
(I was almost driven to insanity trying to get this one fixed!)

Privacy & Terms