Download

Player Respawn Location Problem

I have a little top-down space shooter type thing I’m building and I’ve run into a problem that I’m not sure how to solve. The problem comes when the player dies and respawns. On player death, I am firing a delegate on my GameMode base class. In the delegate a timer is created that waits 10 seconds and then respawns the player. Initially, I was respawning the player like this:



//RestartPlayer(GetWorld()->GetFirstPlayerController());


When that code fired the player respawned, but the ship was in the wrong location at the wrong scale and rotation. I then placed a PlayerStart down to see if the location would at least be right, but it didn’t seem to have any effect. At that point I changed the way I respawned like so:



void AAstroDefender2GameModeBase::OnRespawn()
{
    FActorSpawnParameters params = FActorSpawnParameters();
    params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;

    FVector RespawnLocation = FVector(0.f, 0.f, 10.f);
    FRotator RespawnRotation = FRotator(-90, 0, 180);
    FVector RespawnScale = FVector(.08f, .08f, .08f);
    FTransform transform = FTransform(RespawnRotation.Quaternion(), RespawnLocation, RespawnScale);

    APlayerController* controller = GetWorld()->GetFirstPlayerController();
    controller->UnPossess();
    TSubclassOf<class APlayerShip>* ShipBp = (TSubclassOf<class APlayerShip>*)GetWorld()->SpawnActorAbsolute(PlayerShipBlueprint, transform);
    //RestartPlayer(GetWorld()->GetFirstPlayerController());
    controller->Possess((APawn*)ShipBp);
    PauseSpawning = false;
}


This code appeared to work perfectly. The ship spawns where I want at the appropriate rotation and scale and I can control it properly. The problem comes when I then fire the weapon. When I do, the lasers that it shoots are offset in front of the ship slightly left and right of center. This is the code that fires the weapon:



FVector SpawnLocation = GetActorLocation();
FVector SpawnLocation2 = GetActorLocation();

FActorSpawnParameters SpawnParams;
bool bNoCollisionFail = true;
FVector FireVector = FVector(1, 0, 0);

SpawnParams.SpawnCollisionHandlingOverride = bNoCollisionFail ? ESpawnActorCollisionHandlingMethod::AlwaysSpawn : ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding;

FVector Offset = FVector(0, -30, 250);
FVector Offset2 = FVector(0, 30, 250);

SpawnLocation = this->GetActorLocation() + Offset;
SpawnLocation2 = this->GetActorLocation() + Offset2;

UWorld* const World = GetWorld();
if (World)
{
    const FRotator FireRotation = GetActorRotation();
    AActor* LaserBlast = World->SpawnActor<AActor>(LaserBlastBPClass, SpawnLocation, FireRotation, SpawnParams);
    AActor* LaserBlast2 = World->SpawnActor<AActor>(LaserBlastBPClass, SpawnLocation2, FireRotation, SpawnParams);
}


The problem is that the location where the lasers spawn is way out of position. In fact, they are so far out of position that my code that cleans up laser blasts that are off screen is causing them to get immediately destroyed. I suspect what is happening is that the location that is getting returned from GetActorLocation() is the position where the ship would be if I hadn’t moved it when the respawn happened.

Does anyone know why that is happening and what a possible solution would be?

So somehow, someway, I fixed the problem. I wish I could provide an answer for what I did to fix it in case anyone else has a similar problem, but I honestly have no idea. I don’t believe I’ve changed any code, so perhaps it jsut wasn’t compiling or something… It’s a mystery.