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!