Buggy Aiming

I have still been having an issue with my aiming, I’m not sure if it’s supposed to be like this at this point in time but it’s buggy in the sense that sometimes if I aim somewhere it may not respond and aim where my crosshair is.

Sometimes if I aim to the left and the barrel is closest to the left it sometimes will rotate all the way around or sometimes get stuck halfway to the location, all my code is the same for my aiming component but I can not figure out what the issue is?

You can find my CPP file here: CPP File

and my Header file here: Header File

Took me a little bit but I found your problem. It’s because you have your camera in a way that it’s always looking at the top of the tank, I debuged this by enabling the debug line trace and noticing it hitting the top of the tank, the red arrows (they look like lines in the picture but they’re little arrows) being the normal of the hit.

I added the debug line like this if you’re wondering

const FName TraceTag("TraceTag");
GetWorld()->DebugDrawTraceTag = TraceTag;
FCollisionQueryParams CollisionParams;
CollisionParams.TraceTag = TraceTag;
if (GetWorld()->LineTraceSingleByChannel(
		HitResult,
		StartLocation,
		EndLocation,
		ECollisionChannel::ECC_Visibility,
		CollisionParams)

The solution is to add the tank to the ignored actors for the trace by using the FCollisionQueryParams.

FCollisionQueryParams CollisionParams;
CollisionParams.AddIgnoredActor(GetControlledPawn());
if (GetWorld()->LineTraceSingleByChannel(
	HitResult,
	StartLocation,
	EndLocation,
	ECollisionChannel::ECC_Visibility,
	CollisionParams)
	) 

And now everything is hunky dory :slight_smile:

3 Likes

Thank you so much! Honestly I attempted to draw a debug trace as well to figure it out but I think I put it in the wrong place so I couldn’t actually see what was happening, mine definitely works very well now so thank you so much :slight_smile:

I have still been having an issue with my aiming, I’m not sure if it’s supposed to be like this at this point in time but it’s buggy in the sense that sometimes if I aim somewhere it may not respond and aim where my crosshair is.

Sometimes if I aim to the left and the barrel is closest to the left it sometimes will rotate all the way around or sometimes get stuck halfway to the location, all my code is the same for my aiming component but I can not figure out what the issue is?

That gets covered eventually, not sure about that other problem though.

@DanM I think the first issue is arising because of the hitlocation returning null which happens when the terrain is too far away to tell where its being hit. I believe it gets resolved in a later lecture.

To make sure the turret chooses the shortest rotation path - clockwise vs counter-clockwise you can do this:

float UTankAimingComponent::To360Degrees(float rot) { return rot > 0 ? rot : rot + 360.0f; }

void UTankAimingComponent::MoveTurret(const FVector& AimDirection)
{
	if (!turret) { return; }	
	float aimRotation = To360Degrees(AimDirection.Rotation().Yaw);
	float currentTurretRotation = To360Degrees(turret->GetForwardVector().Rotation().Yaw);

	float relativeRotation = aimRotation - currentTurretRotation;
	/* calculate the opposite rotation: */
	float oppositeRelativeRotation = (360.0f - FMath::Abs<float>(relativeRotation)) * FMath::Sign(relativeRotation) * (-1);
	/* rotate the turret using the shortest path */
	turret->Rotate(
		FMath::Abs<float>(relativeRotation) < FMath::Abs<float>(oppositeRelativeRotation)
		? relativeRotation
		: oppositeRelativeRotation
	);
}

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

P.S. I am pretty sure there should be a simpler solution. This one works though. :slight_smile:

This would be a more simple way to do it:

1 Like

Privacy & Terms