Issue with 2nd Player's Camera

Options
Hey, so I'm not a networking genius, just a forewarning.

I know I'm close to figuring this out, but I just can't seem to put the nail in the coffin on this one.

Basically, I have a prefab for Player 1 and Player 2.

When the room is created, player 1 connects perfectly fine.
When player 2 joins, I call "public void OnPhotonPlayerConnected(PhotonPlayer player)".

My thought process here is basically "if we have 2 players" or "if (PhotonNetwork.playerList.Length == 2)"
Then instantiate the 2nd player.

My issue comes when the player spawns, the GameObject is created, but I'm having troubles figuring out how to tell Photon to use that camera's view. I tried adding Photon View to just about every combination of objects I can think of and it hasn't done anything for me. I've poured over the tutorials, but I'm not seeing the line of code or script on an object that is throwing me off.

Question in a nutshell. How do I tell Unity to use Player2.gameObjects camera? Right now it simply doesn't bind Player2 to a camera and you look at a blank screen.

Any help or pointers in the right direction would be greatly appreciated. Thanks so much :)

Comments

  • Tobias
    Options
    I take it you have a script on a cam that will follow any GameObject you assign to that cam/script?
    If so, PhotonNetwork.Instantiate() return a GameObject which you can set as target of the cam.

    I don't think you should wire up the cam with the Prefab. You would end up having several scattered around and never finding all of them again. It sounds messy to me.

    In the Marco Polo tutorial, I also setup a cam to follow either character. It doesn't matter if it's player1 prefab or player2 prefab you setup as target.
    http://doc.exitgames.com/photon-cloud/M ... o_Tutorial

    One last thing: If the player count is 2, you use the second prefab. You should make sure the room is closed or can no longer be joined. Else you get the case where player 1 leaves and a third user joins and it also will have a player count of 2.
  • Thanks for the response. I'll take a look at that when I get home from class.

    It's an RTS, and my "player" is the actual camera with the player script (with all my movement/dragselect controls, etc).

    From the sounds of it, Are you suggesting I add the player script to an empty gameObject and use the PhotonNetwork.Instantiate() to create that camera-less prefab then having one camera in the scene which has it's target set to that empty gameObject I just created?

    My question with that is if that will mess with player1's camera/view since there would be only one in the scene. (I'll take another look at the tutorials just to make sure).
  • Tobias
    Options
    Each client has only one cam this way, pointing to the "controlled" unit. You likely don't have to sync cam positions and they just show what the local player is doing or you can unlink it from a player/unit and move it locally (pointing maybe at some empty game object you can move around as target)...
  • benkurdziel
    edited September 2013
    Options
    Hey, I did a little work and I talked to a guy at my school who worked on their project with Photon last year, and he gave me some pointers, but I've still got an issue with the PhotonView.

    Basically I've simplified everything down to 1 Player Prefab that is loaded when a player connects.

    So before I post the code, the run down is. A player connects to a Room where they wait until a second player joins. When the second player joins, we load the level and in void Awake () I run the following code:
    [code2=csharp]foreach (PhotonPlayer connectedPlayer in PhotonNetwork.playerList)
    {
    if (photonView.isMine)
    {
    playerID = PhotonNetwork.player.ID;

    Debug.Log ("Creating Player at Awake");
    playerInst = (GameObject)PhotonNetwork.Instantiate("prefab_Player", this.gameObject.transform.position, Quaternion.identity, 0);

    script_Player playerScript = playerInst.GetComponent<script_Player>();
    RtsCamera playerRTSCam = playerInst.GetComponent<RtsCamera>();
    RtsCameraKeys playerRTSCamKeys = playerInst.GetComponent<RtsCameraKeys>();
    RtsCameraMouse playerRTSCamMouse = playerInst.GetComponent<RtsCameraMouse>();
    RtsEffectsUpdater playerRTSEffects = playerInst.GetComponent<RtsEffectsUpdater>();
    Camera playerCam = playerInst.GetComponent<Camera>();
    PhotonView playerView = playerInst.GetComponent<PhotonView>();

    playerScript.enabled = true;
    playerRTSCam.enabled = true;
    playerRTSCamKeys.enabled = true;
    playerRTSCamMouse.enabled = true;
    playerRTSEffects.enabled = true;
    playerCam.enabled = true;
    }
    }[/code2]
    The first player gets his view fine. The second player does not (though the object is created). The Photon view on the 2nd player's object is "owned" by the first player still. Is this my problem? How would I resolve that?

    Or am I going about this in an entirely incorrect way? I feel like I'm right on the edge of having this, but there is something missing that I just can't find.

    Thanks again in advance :)
  • Tobias
    Options
    In Awake() both players should instantiate their own prefab. If you make sure Awake is in a script that's executed by both when loading the scene, both will instantiate their own stuff.
    Don't load the characters with the scene. Those will be scene game objects (as far as networking is related) and those belong only to the master client.
    Create them via PhotonNetwork.Instantiate.

    Check out the Marco Polo tutorial, if you haven't:
    http://doc.exitgames.com/photon-cloud/M ... o_Tutorial