Cone Check/Cone Trace

That shouldn’t lag at all. In my game i do line traces every tick and it’s still not lagging. You should only do one single Line Trace if the Cone Trace found something. This shouldn’t lagg at all. Is the cone trace itself taking too much performance?

It’s a nested loop for each cone trace, so yes I assume it’s the cone trace taking the bulk performance.

Hey there, I just created this tutorial and the first thing I do is create a cone check for the AI. It uses simple maths and you can just loop through all the characters you want to find out which are inside the cone.

Hope it helps!

https://www…com/watch?v=Gsfnf25WAig

Thanks for posting this. A programmer friend of mine basically made the same solution, and I completely forgot that I had asked it here. Your tutorial should point others who were wondering about this in the right direction!

I’m starting in visual scripting but I think what I did is not bad (tell me if it is). I trace a square rotated 45° with a start and an end at the same distance. Then I check the closest actor in the square with a maximum distance from the player character, to respect a cone shape (a square cut in half). The prob is I can only have a cone with fov 90°.

Here is my fonction if it can help :

(yes, start and end are the same, there are two same pieces of script, to adjust if needed)

I know this is old and may not exactly answer OP’s question but for someone looking to do a manual cone trace, but I have one very simple idea :

  1. Cast a sphere.

  2. Sphere’s center = cone’s eye location, Sphere’s radius = scan distance

  3. Use a SphereOverlapActors (you could also use SphereTrace)

  4. Get all hit actors, and do a bit of math to filter the ones that would lie in your cone.

Example Code :

void ATracerDummy::TraceCone()
{
	const float Radius = ConeLength; // Scan Distance
	const FVector& ScanLocation = ScanSocket.GetLocation(); // Start Start or Eye Location
	const FVector& ScanDirection = ScanSocket.Rotator().Vector().GetSafeNormal(); // Eye's looking direction / Eye Forward

	TArray<TEnumAsByte<EObjectTypeQuery>> ObjectTypes;
	ObjectTypes.Add(UEngineTypes::ConvertToObjectType(ECollisionChannel::ECC_Pawn)); // The CollisionChannel of the Target

	TArray<AActor*> IgnoreActors;
	IgnoreActors.Add(this); // Ignore self, or anything else

	TArray<AActor*> HitActors;

	if (UKismetSystemLibrary::SphereOverlapActors(GetWorld(), ScanLocation, Radius, ObjectTypes, AActor::StaticClass(),
	                                              IgnoreActors, HitActors))
	{
		for (int i = 0; i < HitActors.Num(); ++i)
		{
			const FVector& ActorDirection = (HitActors[i]->GetActorLocation() - ScanLocation).GetSafeNormal();

			const float DotProduct = FVector::DotProduct(ActorDirection, ScanDirection);

			const float LowerLimit =  FMath::Cos(FMath::DegreesToRadians(ConeAngle / 2));
			constexpr float UpperLimit = 1.0f;
			
			if (DotProduct >= LowerLimit && DotProduct <= UpperLimit)
			{
				// Your code.
			}
		}
	}
}

This may be fast for a small scan distance and cone angle.

6 Likes