Search Unity

Resolved How do I give each player their own camera?

Discussion in 'Multiplayer' started by Timbecile76, Oct 13, 2010.

Thread Status:
Not open for further replies.
  1. Timbecile76

    Timbecile76

    Joined:
    Jul 8, 2009
    Posts:
    248
    Hi,

    I've been using the zero 2 hero (M2H) networking tutorial to try and wrap my head around the whole multiplayer networking thing.

    I want to change the camera so that each player has their own camera that follows them around.

    I created a player prefab that has a camera attached to it. Each player spawns perfectly, but for some reason, the game only ever follows the last camera that was created.

    I tried looking around, but I couldn't find anything!
     
    ok2010 likes this.
  2. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,027
    disable all cameras by default. enable only the camera with that player's player prefab.
     
    Last edited: Oct 13, 2010
  3. Timbecile76

    Timbecile76

    Joined:
    Jul 8, 2009
    Posts:
    248
    Well that didn't work,....

    here's what I tried:

    All cameras were disabled by default. locally on the each cloned player, I added code that enabled that particular camera.

    I think I know what the problem might be though. I'm using an authoritative server, and the server spawns all the players, and along with them, their cameras.

    I'm just not sure how I should spawn a camera on the client and tell it to follow the client character...
     
  4. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,027
    You'll have to have some way for the client to know what camera/character is supposed to be theirs so they can enable their camera locally (the client needs to execute the code).
     
    Last edited: Oct 13, 2010
  5. Timbecile76

    Timbecile76

    Joined:
    Jul 8, 2009
    Posts:
    248
    that did it!

    just for the sake of posterity, here's what I had to do....

    I created a prefab with the player and the camera.
    I disabled the camera and the audio listener in the prefab.

    I created two vars in the tutorial_3_playerscript:

    Code (csharp):
    1.  
    2. public var myCam : Camera;
    3. public var myAudioListener : AudioListener;
    4.  
    the Update function has an if statement that checks who the script owner is. Inside that statement I added:

    Code (csharp):
    1. function Update(){
    2.    
    3.     //Client code
    4.     if(owner!=null  Network.player==owner){
    5.         //Only the client that owns this object executes this code
    6.         if (myCam.enabled == false)
    7.             myCam.enabled = true;
    8.            
    9.         if (myAudioListener.enabled == false)
    10.             myAudioListener.enabled = true;
    now it works beautifully. Thanks for the help!
     
    HavaDev and RambaMamba like this.
  6. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    thanks, ive been having the same issues. Well done!
     
    Last edited: Nov 2, 2010
  7. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    instead of downloading the enitre M2H project to see what code you modded, can you upload the entire script?
     
  8. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    he gave you all the info needed, tutorial_3_playerscript...
     
    codeBatt likes this.
  9. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    i know but i dont want to have to download that whole project just to get one script from it...
     
  10. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    It has been a while since this thread was last updated, but are you sure you want to run that in Update()?
     
  11. ackyth

    ackyth

    Joined:
    Oct 29, 2009
    Posts:
    146
    Personaly I would put it in a function that gets called when the player joins the game and is finished loading the level. Somewhat wastefull in Update but I dont think its a performance issue with the little thats going on. (unless you do this everywhere)
     
  12. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    Piling things into update can really get ugly fast and should be avoided if possible.

    For the network game I am working on now, each client as a non-networked camera which is bound to their player. It works reasonably well, but I am not pushing for a super secure game as players can freely see the entire map. When you spawn, the camera mounts back to your player and that's that.
     
    AndyTheDishwasher likes this.
  13. Simba

    Simba

    Joined:
    Nov 8, 2010
    Posts:
    77
    Thank you sooooo much for publishing your answer!

    I had something similar, but way too complicated - and it didn't work. This even I can get my head around - so thanks again!

    Arthur
     
  14. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    If you look at the cars or third person examples in the Networking Example package from the Unity3d.com resources section, you'll see the solution is much simpler than you think. You only really need one camera in the scene with a null target variable, and create a script on your player prefab so that when he spawns, check if NetworkView.isMine, and set the camera target to be that player locally.
     
  15. Simba

    Simba

    Joined:
    Nov 8, 2010
    Posts:
    77
    Oops!

    I forgot to check the network tutorial. Thanks for reminding me!

    It is way more simple than I thought. I had a script that created cameras, destroyed cameras, all the works... and, to round it all off, didn't work.

    Thanks legend411!
     
  16. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    No problem dude... This stuff is confusing as hell to me too, but I've been playing with it every day for a few days, and I think I'm finally starting to understand most of it a little better. RPC's are amazing. What kind of game are you making?
     
  17. boco

    boco

    Joined:
    Dec 7, 2010
    Posts:
    373
    This is what I used to snap the camera to a object with a tag of player rifle which is just above the guys gun so it FPS...

    Youtube: http://www.youtube.com/watch?v=OxbWcDxcBsE

    var PlayerScope : GameObject;
    function LateUpdate()
    {

    //Creates an array of gameobjects that have tag "PlayerRifle"
    var rifleArray : GameObject[] = GameObject.FindGameObjectsWithTag("PlayerRifle");



    for(rifle in rifleArray)
    {
    if(rifle.networkView.isMine)
    {
    PlayerScope = rifle;
    }
    }
    if(PlayerScope.networkView.isMine)
    {
    transform.position = PlayerScope.transform.position;
    transform.rotation = PlayerScope.transform.rotation;
    }
    }
     
  18. shinares

    shinares

    Joined:
    Apr 28, 2013
    Posts:
    15
    Hi sorry for the noob question but how to do this ? What you meant is not add component isnt it ?
     
  19. Faltren

    Faltren

    Joined:
    Jan 10, 2017
    Posts:
    17
    I use Unet (the newst one) and I had the same issue. It works perfectly for me ! thx !
     
  20. moco2k

    moco2k

    Joined:
    Apr 29, 2015
    Posts:
    294
    Instead of Update, you can just put the camera assignment code into OnStartLocalPlayer.
     
    GoDJaMMing likes this.
  21. Agent-6141

    Agent-6141

    Joined:
    Mar 17, 2013
    Posts:
    3

    Any chance you can make that work with my code?

    Code (CSharp):
    1. using UnityEngine.Networking;
    2. using UnityStandardAssets.Characters.ThirdPerson;
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class LocalPlayerControl : NetworkBehaviour {
    7.  
    8.     public Camera cam;
    9.  
    10.      void Start()
    11.     {
    12.  
    13.  
    14.         if (isLocalPlayer)
    15.             GetComponent<ThirdPersonUserControl>().enabled = true;
    16.             GetComponent<ThirdPersonCharacter>().enabled = true;
    17.             GetComponent<Animator>().enabled = true;
    18.             GetComponent<CapsuleCollider>().enabled = true;
    19.             GetComponent<AudioSource>().enabled = true;
    20.             GetComponent<NetworkAnimator>().enabled = true;
    21.             GetComponent<NetworkTransform>().enabled = true;
    22.             cam.GetComponent<CameraControl>().enabled = true;
    23.             GetComponentInChildren<Camera>().enabled = true;
    24.        
    25.  
    26.        
    27.     }
    28.  
    29.  
    30. }
    31.  
     
  22. diamondisthebest

    diamondisthebest

    Joined:
    Oct 4, 2023
    Posts:
    2
    how
    did u make it work with unet
     
  23. CodeNinja-

    CodeNinja-

    Unity Technologies

    Joined:
    Feb 22, 2023
    Posts:
    27
    Hi @diamondisthebest,
    UNet is a deprecated solution, have a look at this blog to choose the right Netcode for your game.
    Thanks!
     
Thread Status:
Not open for further replies.