Hit multiple objects with Raycast

Does anyone know how to get multiple objects from a LineTraceMulti? I cast a Ray from the Player upwards its UpVector and want to blend out any object that is hit. It works fine when there is only one object above the player character. its gets hidden. The next one doesnt get hit.

I tried to FCollissionResponseParams already and tried to set the ECollisionResponse, but it doesnt really work. If I set it to anything other than ECR_Block, I dont get any hit at all.

Does anyone have an idea?

Thanks
Markus



                FCollisionResponseParams ResponseParam(ECollisionResponse::ECR_Block);           // I only get a hit when I set this to ECR_Block, all other parameters dont result in ANY hit at all
		
		TArray<FHitResult> HitResults;
		//LineTraceMulti(TArray<struct FHitResult>& OutHits
		bool DidTrace = GetWorld()->LineTraceMulti(HitResults, PlayerPosition, PlayerPosition + UpVector * 500.0f, ECC_WorldDynamic, QueryParams, ResponseParam);
		if (DidTrace)
		{
			TArray<FHitResult>::TConstIterator it = HitResults.CreateConstIterator();
			for (; it != NULL; ++it)
			{
			//	if (it->bBlockingHit == true)
			//	{
					DrawDebugLine(GetWorld(), PlayerPosition, PlayerPosition + UpVector * 500.f, FColor::Green, false, -1, 0, 5.f);
					AActor *Actor = it->GetActor();
					if (Actor)
					{
						Actor->SetActorHiddenInGame(true);
						CurrentlyHiddenActors.Add(TWeakObjectPtr<AActor>(Actor));
					}
			//	}
			}
			
		}


EDIT:

OK I figured it out. LineTraceMulti returns a Boolean if and only if HitResults has ANY Blocking Object in it. If we set ECollisioNResponse to ECR_Overlap, it will now return false of course.

I solved it like this:


    FCollisionResponseParams ResponseParam(ECollisionResponse::ECR_Overlap);

		
		TArray<FHitResult> HitResults;
		
		GetWorld()->LineTraceMulti(HitResults, PlayerPosition, PlayerPosition + UpVector * 500.0f, ECC_WorldDynamic, QueryParams, ResponseParam);
		if (HitResults.Num() > 0)
		{
			TArray<FHitResult>::TConstIterator ActorIt = HitResults.CreateConstIterator();
			for (; ActorIt != NULL; ++ActorIt)
			{
				DrawDebugLine(GetWorld(), PlayerPosition, PlayerPosition + UpVector * 500.f, FColor::Green, false, -1, 0, 5.f);
				AActor *Actor = ActorIt->GetActor();
				if (Actor)
				{
					Actor->SetActorHiddenInGame(true);
					CurrentlyHiddenActors.Add(TWeakObjectPtr<AActor>(Actor));
				}
			}
					
		}

1 Like

That is correct, LineTraceMulti will only ever return one blocking reault, but possibly multiple overlap results. This can be a big optimization, as we don’t need to look for any collisions past that blocking result.

1 Like