My turret doesn't aim where it should, why?

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()
	Tank = Cast<AClassTank>(GetParentComponent()->GetOwner());

// Called every frame
void ATurret::Tick(float DeltaTime)

	// Aim!
	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;
						// It can't turn in one frame so turn as mush.
						CurrentRotation.Yaw += (MaxDeltaYawThisFrame * FMath::Sign(DeltaYaw));

Could someone light a sparkle in my life?
Any help is well appreciated, if I didn’t give enough info just let me know.

It’s just off by 90 degrees. Assuming your UTankStatics::FindAngleDegrees is implemented correctly, the most obvious cause would be that your turret component (mesh? sprite?) is just not setup correctly. You want the barrel to be pointing down the local X axis.

Ye, I did that, so the barrel is pointing down the X axis, but it has a ArrowComponent attached to it, so the barrel is aiming on the right direction, but the arrowcomponent which is what will be used to fire the bullets is still off by 90 degrees.

How to I rotate an arrow component? I can only rotate the sprite of the turret :frowning:

I’m new to this, so I might be wrong, I don’t know if the arrowcomponent is only attached to to the tank when the game starts, in which case it would be facing the same direction as the turret sprite, I’ll check that tomorrow as I don’t have the time right now.

Thanks for the reply, kamrann, I appreciate it.

Hi, I know it’s been a few months since you asked this, but I just want to answer the question so otther people who run into the same problem can fix it. I had the same problem, and I solved it just by changing the rotation around the z-axis from 0 to 90 in the class defaults of the Turret.

I have the same exact problem. Rotating the turret 90 degrees on the z-axis isn’t working because in order for the missiles to shoot they need the TurretDirection rotation. When you rotate the turret it makes the missiles shoot 90 degrees from the turret.