CollisionBox for target selection within a certain radius?

Hey,

The player should be able to select/switch throught targets near his character.
e.g.: Player presses “TAB” and he will select the nearest target within a special radius around his character, without any height limit. So even if the the would be 10km above the target, if he is within this radius around his character he will be able to select the enemy.

Is a collision box the right approach for this?

If your player needs to get a single Actor above him a trace would suffice. You could easily just run a trace above your player and use the Hit Actor in your hit results. Whats not shown in this example is to switch between the targets. You could do this by doing a sphere trace for object instead of by channel and add the previously targeted actors to the ignore list. Heres the documentation page on traces (raycasts).

(Posting this as an answer as comments cannot accept all my text…)

This question is posted in C++ so I assume op might be looking for a code solution as well as Blueprints.

I would implement it slightly differently in any case - according to op’s specific requirements (Z axis difference should not affect the output), I would suggest implementing an interface on all targetable objects, and then to use an actor (or object) iterator to find the relevant targetable objects. Using an interface would speed up this query significantly.

After getting the list of targetable objects, you want to find the distance from the player to each of these, ignoring the Z component. This can be done as follows (I’m assuming the objects are actors for simplicity’s sake):

AYourCharacter::GetClosestTarget()
{
	// I'm assuming you already
	TArray<Actor*> targetableActors = ...
	
	// Init closest actor
	AActor* closestActor = nullptr;

	// Only do if there are actually 
	if(targetableActors.Num() > 0)
	{
		// Get character location
		FVector myLoc = GetActorLocation();

		// Use first actor as the closest one
		closestActor = targetableActors[0];

		float firstLoc = closestActor->GetActorLocation();	
		// Ignore Z-axis	
		firstLoc.Z = myLoc.Z;		

		// Calc first closest distance squared - we use squared as there's no need to root the distance for comparison purposes
		float closestDistSqrCurrent = FVector::DistSquared(myLoc, firstLoc);
		
		for(int32 i = 1; i < targetableActors.Num(); i++)
		{
			// Get current targetable actor location
			FVector targetableActorLoc = targetableActors[i].GetActorLocation();

			// Ignore Z axis
			targetableActorLoc.Z = myLoc.Z;
			
			// Get squared distance from character to targetableActor
			float distSqr = FVector::DistSquared(myLoc, targetableActorLoc);
			if(distSqr < closestDist)
			{
				closestDistSqrCurrent = distSqr;
				closestActor = targetableActors[i];
			}
		}

		// Do something with the closest actor
		closestActor->Target()...
		const float closestDist = FMath::Sqrt(closestDistSqrCurrent);
		...
	}
	else
	{
		// there were no available actors...
	}
}