Previously, I just spawned based on the location of the pawn - literally with GetActorLocation - which, while obviously not ideal and causes AI pathfinding issues (object occasionally spawning inside spawning AI) it worked well enough for prototyping and making sure that the AI functioned correctly etc.
I was wondering, how would I go about spawning behind the actor a couple of units?
I’m not really a vector magician, but the first thing comes to my mind is to rotate the ForwardVector of the OtherActor 180 and multiply it by the distance something like:
You would want to do some traces to determine that. It really depends on how complex you want it to make, but for a basic system, do a simple LineTrace from the back of the OtherActor to the SpawnLocation. If it was a bBlockingHit, then the ray could not reach the SpawnLocation->something is in the way.
I’m not sure what you mean. I was assuming that you want to spawn an actor X distance behind another actor, and check if there is no wall between the two actors.
If you do a line trace from the ‘OtherActor’ to the ‘SpawnLocation’, the ray should travel from ‘OtherActor’ to ‘SpawnLocation’. If there is anything blocking the ray, the bBlockingHit flag will be set to true, so you know that there is something between the two actors. I’m not sure how distance comes into play?
This is how I’d do the trace, assuming the ‘OtherActor’ is the Player:
//Retrieving the Player Character from the Player Controller.
APlayerClass* MyPlayer = Cast<APlayerClass>(MyPlayerController->GetCharacter());
if (MyPlayer != nullptr && GetWorld() != nullptr)
{
//Initializing the Start Location of the trace to the world location of the "Player".
const FVector TraceStart = MyPlayer->GetActorLocation();
//Initializing the End Location of the trace to the previously calculated 'SpawnLocation'.
const FVector TraceEnd = SpawnLocation;
//Inititalizing the Line Trace Parameters.
FCollisionQueryParams TraceParameters = FCollisionQueryParams(FName(TEXT("Trace Parameters")));
TraceParameters.bTraceComplex = false;
TraceParameters.bTraceAsyncScene = true;
TraceParameters.bReturnPhysicalMaterial = false;
//Initializing the Line Trace ignore list.
TArray<AActor*> TraceIgnoreList;
TraceIgnoreList.Add(MyPlayer);
TraceParameters.AddIgnoredActors(TraceIgnoreList);
//Initializing the "TraceHitResult".
FHitResult TraceHitResult(ForceInit);
//Performing the actual trace on the 'Visibility' channel.
GetWorld()->LineTraceSingleByChannel(TraceHitResult, TraceStart, TraceEnd, ECC_Visibility, TraceParameters);
//If the trace was not a blocking hit.
if (TraceHitResult.bBlockingHit == false)
{
//There is nothing blocking the ray between the two locations, we can spawn out actor at "SpawnLocation".
}