1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

How do I give each player their own camera?

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

  1. Timbecile76

    Timbecile76

    Joined:
    Jul 8, 2009
    Posts:
    234
    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!
     
  2. JTown

    JTown

    Joined:
    Jan 9, 2010
    Posts:
    983
    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:
    234
    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. JTown

    JTown

    Joined:
    Jan 9, 2010
    Posts:
    983
    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:
    234
    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!
     
  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,685
    he gave you all the info needed, tutorial_3_playerscript...
     
  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.
     
  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,002
    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,002
    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:
    371
    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:
    227
    Instead of Update, you can just put the camera assignment code into OnStartLocalPlayer.