Server/Client Camera Spawns Different

So I’m working on adding networking to my game which uses a shared camera similar to Smash Bros. or Little Big Planet. Currently this is how I’m spawning and setting the camera view:

//in AMyPlayerController.h

void AMyPlayerController::BeginPlay()
{
	Super::BeginPlay();

	SetViewTarget(GetWorld()->SpawnActor<AMyLevelCamera>());
}

When testing the networking in the editor, this works for the server player, and at the start of the game the client players show the view of this camera for a split second, but after that, the client players switch to a default player camera after about one frame.

I’m wondering what the proper way of spawning a camera like this is, so I can fix this.

1 Like

In GameMode, you will spawn Player Controller(PC) when you LogIn GameMode, BeginPlay of PC will be call, then GameMode will see if you have enough player it then will call StartMatch which will call RestartPlayer to spawn the default Pawn for all PC. So basically you must call ViewTarget after GameMode call RestartPlayer, or can prevent the function to be called. You can add CanRestartPlayer in PlayerController script, it will prevent call RestartPlayer, or just simple put Timer to call set view after few second.

Other thing you can do it, when PC pawn DefaultPawn it will Possessed that Pawn so you can just override the PlayerController::Possessed() let the Super::Possessed finish first then call your SetViewTarget, btw all this thing will run on server side only.

2 Likes

btw all this thing will run on server side only.

Yeah, I just tested what you suggested with PlayerController::Possess() and it seems that’s the case.

The problem I’m having is that client players briefly show a zoomed out view before the camera gets set to the default one (pictured top). With SetViewTarget in PlayerController::BeginPlay(), it already works fine for the server player (pictured bottom).

I think the reason it resets after one frame is something like this:
Client Spawns PC:

  • calls PC::BeginPlay()
  • sets camera to correct one
    Then,
    Server Spawns copy of client actor:
  • resets it to the default camera
  • replicates the new camera to the client

I’m not sure how to fix this aside from using something gimmicky like a timed delay. I think there’s probably some appropriate function where I should do this, but I’m not sure what it is.

I’m not sure what the etiquette is about ‘bumping’ here, but I’m still having this problem. Where should I be setting the camera/viewtarget in a networked game?

So I found a variable “bAutoManageActiveCameraTarget” in the PlayerController class and set it to false in the constructor. It seems to have fixed the problem of switching cameras.

1 Like