2017-04-19 49 views
0

我目前正在研究一個項目,其中所有我的玩家使用不同的攝像頭。與虛幻引擎聯網的攝像頭問題4

我首先想到的是使用UCameraComponent,但是每個攝像頭都需要轉動某個點而不是隨着棋子的移動而移動。

所以我決定在我的棋子的BeginPlay()中產生一個相機Actor。

void AMyCharacter::BeginPlay() 
{ 
Super::BeginPlay(); 
if (!hasCamera) { // Camera not set yet 
    FVector vectpos; // Target position of the camera 
    vectpos.X = -1130; 
    vectpos.Y = 10; 
    vectpos.Z = 565; 
    FRotator rotation; 
    rotation.Pitch = -22; 
    rotation.Yaw = 0; 
    rotation.Roll = 0; 


    APlayerController* controller = Cast<APlayerController>(GetController()); 

    if (controller == NULL) // When I'm on client, the GetController() return NULL. 
    { 
    // Trying to find the controller of my client 
     for (FConstPlayerControllerIterator Iterator = GetWorld()->GetPlayerControllerIterator(); Iterator; ++Iterator) 
     { 
      controller = *Iterator; 
      //On client, there is only 1 controller according to the documentation. 
     } 
    } 

    if (controller != NULL) 
    { 
     controller->SetViewTarget(Camera); // Set the view with the new camera 
    } 
    SetCamera(true); // Call and RPC Function to update the hasCamera variable 
} 
} 

這是爲第一個球員工作,之後它取決於。有時候,第二個玩家會得到一臺可以正常工作的相機,但有時候,他通過錯誤的相機觀看,並且Camera變量與他正在查看的不一樣。有時,當新玩家加入遊戲時, /第二位玩家通過錯誤的相機看。

這裏是我們用來讓局域網連接bewteen在客戶端和服務器(第一客戶端創建遊戲)的GameInstance藍圖 GameInstance Blueprint

如果有人能找到原因時,相機不工作,這將是非常好的!非常感謝您的幫助。

+0

網絡服務器有所有的播放器控制器,你總是拿最後一個。嘗試使用'UEngine :: GetFirstLocalPlayerController' –

+0

感謝您的回覆,我嘗試了這一點,它適用於服務器,但只要我連接到第二個播放器,我的服務器就會丟失他的相機。當我連接第三名球員時,每個人都會失去他的相機。這是否是因爲BeginPlay正在客戶端和服務器上調用,並且服務器將Camera變量的值更改爲錯誤的?有沒有辦法讓我們正在瀏覽的當前相機? – Matriac

+0

將在服務器上爲每個玩家調用'BeginPlay'。起初,「APawn」具有「GetController」功能。所以你不需要在某個地方尋找它。其次,'AController'具有'IsLocalController',所以你可以找出它是本地還是網絡播放器控制器。 –

回答

0

顯然,您選擇了錯誤的方式。

在UE4'ACharacter'(準確地說是APawn)是世界上的角色表示,所以每個玩家都會有一個角色代表。因此,將相機代碼放入它是很奇怪的。

您應該製作自己的控制器(例如'AMyPlayerController')並從中控制相機。顯然,只適用於本地球員。