Search Unity

[Solved] Problem with new instantiated players with cameras

Discussion in 'Multiplayer' started by Rodrigo G., Feb 1, 2010.

  1. Rodrigo G.

    Rodrigo G.

    Joined:
    Dec 9, 2009
    Posts:
    7
    Hi people!
    I've read some old threads about this same issue but haven't found an answer yet.

    The thing is: I'm following the network tutorial from Leepo and doing some modifications, trying to finally have a FPS game.

    Running the tutorial project with the cubes successfully, the only two things I've changed are, first I've erased the main camera, and then I've used a ship, with a camera attached to it, instead of the cube to be my network instantiated object. Then, when I host a server, the ship spawns correctly, I can see through the camera on the ship and I'm able to move the ship just fine.

    The problem happens when a client connects to the game and a new ship instantiates. Now, the host sees through the client's new ship camera and the client sees through the host's previous ship camera, but the controls of both ships are in the correct way.

    I guess the problem here is that the host (server) always looks through the most recently created camera. (I don't really know if that's the answer).

    I've read the scripts from Leepo's FPS Example, and tried to understand how he manages to do the thing, but I'm not an experienced programmer and cannot see the solution.

    I'm asking for some way to tell the new instantiated network, or the first one, to stay with it's own object's camera. Or perhaps, a way to tell to all Instantiated Objects to ignore other cameras. I really don't know what I must do.

    I would appreciate if someone can give me some kind of clue in this matter.

    Thanks.
    :wink:
     
  2. Rodrigo G.

    Rodrigo G.

    Joined:
    Dec 9, 2009
    Posts:
    7
    Hi again!!

    I finally finished reading all the thread on Leepo's tutorial post and at the very end (today), there is a post of JTown, and thanks to him, that helped me a lot on solving my problem.

    I could turn off all the cameras on the Update function on the control structure if(network.isMine){ with the following code:

    Code (csharp):
    1. function Update(){
    2.        
    3.     if(networkView.isMine){
    4.         //Only the owner can move the cube!        
    5.         var moveDirection : Vector3 = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    6.         var speed : float = 5;
    7.         transform.Translate(speed * moveDirection * Time.deltaTime);
    8.        
    9.         //find all cams and disable
    10.          allCams = FindObjectsOfType(Camera);
    11.          for (var cam : Camera in allCams) {
    12.             cam.enabled = false;
    13.          }
    14.          //turn on own cam
    15.          var myCamO = this.gameObject;
    16.          var myCam = myCamO.camera;
    17.          myCam.enabled = true;
    18.     }
    19.    
    20. }
    21.  
    You need only to add the last 10 lines, supposed that you attached a camera on the desired object: Selecting the object and adding a Camera from the menu: Component/Rendering/Camera.

    I first do it in other way that didn't work: I added anoher object created form GameObject/Create other/Camera, and then dragged it on my Ship Object.

    Thanks again to JTown.