Pawn elements into overlapping components

Hey world !

I search to move all components into a sphere. So I use the GetOverlappingComponents but I’m a problem with pawns :


	TArray<UPrimitiveComponent*> CollectedActors;
	Sphere->GetOverlappingComponents(CollectedActors);

	for (int32 i = 0; i < CollectedActors.Num(); ++i)
	{
		UPrimitiveComponent* anActor = CollectedActors*;
		if (anActor && !anActor->IsPendingKill())
		{
			anActor->AddForce((GetActorLocation() - anActor->GetComponentLocation()).SafeNormal() * power);
		}
	}

The AddForce function (or AddImpulse) don’t work with pawn component (of ACharacter type, but the debugger show me UCapsuleComponent).

So I try to do this :


	TArray<UPrimitiveComponent*> CollectedActors;
	Sphere->GetOverlappingComponents(CollectedActors);

	for (int32 i = 0; i < CollectedActors.Num(); ++i)
	{
		UPrimitiveComponent* anActor = CollectedActors*;
		if (anActor && !anActor->IsPendingKill())
		{
			APawn* character = Cast<APawn>(anActor);

			if (character)
			{
				character->AddMovementInput((GetActorLocation() - anActor->GetComponentLocation()).SafeNormal(), power);
			}
			else
			{
				anActor->AddForce((GetActorLocation() - anActor->GetComponentLocation()).SafeNormal() * power);
			}
		}
	}

But nothing, because character is never casting.

Are you a solution to find the character of primitive component ?
I think loop into all pawn’s engine in this function, and apply the translation, but I search a better solution if exist.

Thank you.

Hello!

Here’s what’s happening: GetOverlappingComponents() does not return any full Pawns (or Characters) that your sphere overlaps, but it returns the individual components (which can be parts of a Pawn/Character) that overlap with the sphere. What you’ll want to do is loop through your components, use GetOwner() to determine the Actor that owns this component, and then try casting that Actor to APawn to determine whether or not it’s a Pawn.

Personally I’d also recommend changing some of your variables names to better reflect what they are (for instance, rename CollectedActors to CollectedComponents since it is filled with Components, NOT with Actors).

Ok ! I’ll try that.
Thank you.

I understand for the variables names. It’s because I tried first with GetComponentsActors function. And I doesn’t change their.
After the post I found a way with this method. But I some problem too (It’s an another subject). I’ll post my example with GetComponentsActors later when I’ll come back in my home.

Thanks again.

My exemple with GetOverlappingActors (and no GetComponentsActors ^^).


	TArray<AActor*> CollectedActors;
	GravitySphere->GetOverlappingActors(CollectedActors);
	for (int32 i = 0; i < CollectedActors.Num(); ++i)
	{
		AActor* anActor = CollectedActors*;
		if (anActor && !anActor->IsPendingKill())
		{
			APawn* character = Cast<APawn>(anActor);

			if (character)
			{
				// this line code has not tested
				character->GetMovementComponent()->AddRadialForce(GetActorLocation(), 0.0f, power, RIF_Constant);
			}
			else
			{
				UPrimitiveComponent* component = anActor->GetRootPrimitiveComponent();
				component->AddForce(((GetActorLocation() - component->GetComponentLocation()).SafeNormal()) * power);
			}
		}
	}

So your solution is also ok for me.