0

I am currently working on a project where all my players use different Camera.

I first thought using UCameraComponent, but each camera has to turn around a certain point and not moving with the movement of the pawns.

So I decided to Spawn a Camera Actor in the BeginPlay() of my pawn.

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 
}
}

This is working for the first player, and after that it depends. Sometimes, the second player get a camera that works fine, but sometimes, he is viewing through the wrong camera and the Camera variable is not the same one he is looking in. Sometimes, when a new players join the game, it make the first/second player looking through the wrong camera.

Here is the GameInstance Blueprint we use to make the LAN Connection bewteen the clients and the server(the first client to create the game) GameInstance Blueprint

If someone can find why the camera is not working as expected , it would be very nice ! Thanks you all in advance for your help.

Matriac
  • 382
  • 3
  • 11
  • Network server has all player controllers, you always take the last one. Try to use `UEngine::GetFirstLocalPlayerController` – Michael Nastenko Apr 19 '17 at 08:58
  • Thanks for reply, I tried this and it works for the server, but as soon I connect with the second player, my server loses his camera. When I connect with a third player, everyone loses his camera. Could it be because the BeginPlay is being call on the client and on the server, and the server changes the value of the Camera variable for a wrong one? Is there a way to get the current camera we are looking through it? – Matriac Apr 19 '17 at 13:52
  • `BeginPlay` will be called on the server for every player. At first - `APawn` has function `GetController`. So you don't need to look for it somewhere. Second, `AController` has `IsLocalController`, so you can find out is it local or network player controller. – Michael Nastenko Apr 20 '17 at 01:18
  • I already tried these, `GetController()` Only works for the server and every client that called it will get a NULL instead of his controller, i tried looking for the local controller but it was worse , I was checking all of the controllers and get the one that was local, then spawn a camera, but at the end, all my clients had no camera. – Matriac Apr 20 '17 at 01:25
  • 1
    I think you doing it wrong. `ACharacter` is a character representation in the world, so you will have one for every single player. Thus, it is strange to put your camera code in it. IMO, you should make `AMyPlayerController` and control camera from it. Obviously, only for a local player. – Michael Nastenko Apr 20 '17 at 01:43
  • Wow you are totally right lol I got it worked this way! Thank you, this sounds really clear to me now ! You can put this as an answer and i'll mark it as solved. – Matriac Apr 20 '17 at 03:13

1 Answers1

0

Apparently, You choose the wrong way.

In UE4 'ACharacter' (APawn to be precise) is a character representation in the world, so you will have one for every single player. Thus, it is strange to put your camera code in it.

You should make your own controller (ex. 'AMyPlayerController') and control camera from it. Obviously, only for a local player.

Michael Nastenko
  • 2,785
  • 1
  • 10
  • 14