Hey, I’m new to UE4 and recently started to watch the Tanks vc Zombies C++ livestream videos thats on UE’s yt channel and I’m stuck right now, so I thought someone could help me.
The thing is, I have a Tank that has a turret attached to it. And I need to make the turret always point towards my mouse cursor, cause it’ll be the turret that will shoot projectiles, so its basically the aim.
I’ve come to a point where I can make the turret move according to my cursor but it is not “aiming” towards my mouse cursor, there is a space or gap (I don’t know what to call it, english is not my native lang :p) between the turret and the cursor, let me show you a video to hopefully make it more clear.
Basically the turret doesn’t aim at the cursor, I’ve tried copying the code that they uploaded, thinking I could’ve missed something, and it still doesn’t aim properly, same thing happens.
Here is the part of the code that matters:
void ATurret::BeginPlay()
{
Super::BeginPlay();
Tank = Cast<AClassTank>(GetParentComponent()->GetOwner());
check(Tank);
}
// Called every frame
void ATurret::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// Aim!
check(TurretDirection);
if (Tank)
{
if (APlayerController* PC = Cast<APlayerController>(Tank->GetController()))
{
FVector2D AimLocation;
if (PC->GetMousePosition(AimLocation.X, AimLocation.Y))
{
FVector2D TurretLocation = FVector2D::ZeroVector;
UGameplayStatics::ProjectWorldToScreen(PC, TurretDirection->GetComponentLocation(), TurretLocation);
float DesiredYaw;
if (UTankStatics::FindLookAtAngle2D(TurretLocation, AimLocation, DesiredYaw))
{
FRotator CurrentRotation = TurretDirection->GetComponentRotation();
float DeltaYaw = UTankStatics::FindAngleDegrees(CurrentRotation.Yaw, DesiredYaw);
float MaxDeltaYawThisFrame = YawSpeed * DeltaTime;
//UE_LOG(LogTemp, Warning, TEXT("X: %f, Y: %f, YAW: %f"), AimLocation.X, AimLocation.Y, DeltaYaw);
// Perform the current frame's rotation.
if (MaxDeltaYawThisFrame > FMath::Abs(DeltaYaw))
{
// It can go all the way to the desired angle in one tick.
CurrentRotation.Yaw = DesiredYaw;
}
else
{
// It can't turn in one frame so turn as mush.
CurrentRotation.Yaw += (MaxDeltaYawThisFrame * FMath::Sign(DeltaYaw));
}
TurretDirection->SetWorldRotation(CurrentRotation);
}
}
}
}
}
Could someone light a sparkle in my life?
Any help is well appreciated, if I didn’t give enough info just let me know.