Today I worked on an exciting feature that allows the player to dock with a station or other object. When docked, the player hops out of the ship and runs around to perform various sundry tasks like buying, selling, upgrading the ship and exploring.
Here’s what our little Arconian pilot will look like when he’s out of the ship:
[sketchfab]8aa0c376e281470eb85d02502ea8f92e[/sketchfab]
At the core, this is nothing more than unpossessing one pawn and possessing the other. The player controller manages all of this and is where the actual functions to dock/undock exist. Here is a look at one of my station blueprints. You can see that each station has 3 unique things:
- The parking spot for the player ship
- The starting location of the player pawn
- A camera
&stc=1
When the player requests to dock, I do the following:
- Store the ship and camera location for undocking
- Unpossess the ship pawn
- Move the ship pawn to his parking spot
- Spawn/possess the player pawn
- Blend camera to the station blueprint’s camera
Undocking is basically the reverse
- UnPossess the player pawn and destroy
- Move the ship to the entry location stored in #1 of docking
- Possess ship
- Blend camera to ship pawn
One issue I ran into while blending cameras using SetViewTargetWithBlend is that it couldn’t handle blending to a Camera Component My work around was to spawn a helper camera actor in my player controller in BeginPlay():
TransitionCamera = GetWorld()->SpawnActor<ACameraActor>(ACameraActor::StaticClass());
Then, again in my player controller, I made an override for SetViewTargetWithBlend that detects if we’re blending to a space station actor and if so sets up the transition camera based on the camera contained in the blueprint.
void AShipPlayerController::SetViewTargetWithBlend(class AActor* NewViewTarget, float BlendTime, enum EViewTargetBlendFunction BlendFunc, float BlendExp, bool bLockOutgoing)
{
ASpaceStationActor* Space = Cast<ASpaceStationActor>(NewViewTarget);
if (Space)
{
// It's a space station! Setup parameters on the transition camera to match the space station camera and then transition to it
TransitionCamera->GetCameraComponent()->FieldOfView = Space->Camera->FieldOfView;
TransitionCamera->SetActorLocation(Space->Camera->GetComponentLocation());
TransitionCamera->SetActorRotation(Space->Camera->GetComponentRotation());
Super::SetViewTargetWithBlend(TransitionCamera, BlendTime, BlendFunc, BlendExp, bLockOutgoing);
}
else
{
Super::SetViewTargetWithBlend(NewViewTarget, BlendTime, BlendFunc, BlendExp, bLockOutgoing);
}
}