Search code examples
c++unreal-engine4

DontSpawnIfColliding doesn't check actor spawn by the SpawnActor function


 void USpawner::Spawn(TSubclassOf<AMyActor> myactor)
 {
     UWorld* World = GEngine->GameViewport->GetWorld();
     ACharacter* myCharacter = UGameplayStatics::GetPlayerCharacter(World, 0);
     FVector loc = myCharacter->GetActorLocation();
     FActorSpawnParameters ActorSpawnParameters;
     ActorSpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::DontSpawnIfColliding;

     AMyActor* NewActor = World->SpawnActor<AMyActor>(myactor, loc, FRotator::ZeroRotator, ActorSpawnParameters);
 }

This code works fine to spawn the first actor and it checks against existing objects so it doesn't spawn where it may collide with something. However the second time I spawn an object, I notice the object spawning from this function is not taken into account when checking for collisions.

What I'm doing wrong?


Solution

  • When using the SpawnActor method, sometimes the default strategy is not useful. Instead you should both set a Collision Preset and (in search for the peace of mind) a Collision Channel, double checking the Collision Response is not ignoring actors from the same class.