Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

2D Multiplayer Cameras

Discussion in 'Multiplayer' started by AlanBernard, Aug 27, 2018.

  1. AlanBernard

    AlanBernard

    Joined:
    Aug 26, 2018
    Posts:
    2
    Hello, I have an issue with my Unity game. I want it to make a camera for every player that joins since it's a Multiplayer game, but it hasn't worked for me and I've been stuck on this problem for 3 hours now. I've tried 8/9 different methods and I've looked at plenty of threads, so please help me out if you can.

    Player Code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class PlayerController : NetworkBehaviour
    6. {
    7.  
    8.     public float speed;             //Floating point variable to store the player's movement speed.
    9.  
    10.     private Rigidbody2D rb2d;       //Store a reference to the Rigidbody2D component required to use 2D Physics.
    11.  
    12.     // Use this for initialization
    13.     void Start()
    14.     {
    15.         //Get and store a reference to the Rigidbody2D component so that we can access it.
    16.         rb2d = GetComponent<Rigidbody2D>();
    17.     }
    18.     void Update()
    19.     {
    20.         if (!isLocalPlayer)
    21.         {
    22.             return;
    23.         }
    24.     }
    25.     //FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
    26.     void FixedUpdate()
    27.     {
    28.  
    29.         if (Input.GetKey(KeyCode.LeftArrow))
    30.         {
    31.             transform.position += Vector3.left * speed * Time.deltaTime;
    32.         }
    33.         if (Input.GetKey(KeyCode.RightArrow))
    34.         {
    35.             transform.position += Vector3.right * speed * Time.deltaTime;
    36.         }
    37.         if (Input.GetKey(KeyCode.UpArrow))
    38.         {
    39.             transform.position += Vector3.up * speed * Time.deltaTime;
    40.         }
    41.         if (Input.GetKey(KeyCode.DownArrow))
    42.         {
    43.             transform.position += Vector3.down * speed * Time.deltaTime;
    44.         }
    45.  
    46.     }
    47. }
    Camera Code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5.  
    6. public class CameraController : NetworkBehaviour
    7. {
    8.  
    9.     public GameObject player;       //Public variable to store a reference to the player game object
    10.  
    11.  
    12.     private Vector3 offset;         //Private variable to store the offset distance between the player and camera
    13.  
    14.     CameraController mycam;
    15.  
    16.     // Use this for initialization
    17.     void Start()
    18.     {
    19.         //Calculate and store the offset value by getting the distance between the player's position and camera's position.
    20.         offset = transform.position - player.transform.position;
    21.     }
    22.  
    23.     // LateUpdate is called after Update each frame
    24.     void LateUpdate()
    25.     {
    26.             // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
    27.             transform.position = player.transform.position + offset;
    28.     }
    29. }
     
  2. AlanBernard

    AlanBernard

    Joined:
    Aug 26, 2018
    Posts:
    2
    I did it! I'll be sure to reply to this thread or make a new one in case I ever have any more issues, but it was a few vector problems that saved the day!
     
  3. sircv

    sircv

    Joined:
    Aug 10, 2018
    Posts:
    6
    Seems like you got it, but for future reference, what I do is disable the camera attached to the player prefab by default, then in awake of a script attached to the player, if this.isLocalPlayer, enable the camera
     
    AlanBernard likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I wouldn't do it this way. Just have a single camera in your scene, or instantiate it, and in Start on your Player prefab have it check if isLocalPlayer is true, and if so then attach the camera object to itself or reference itself for following.
     
    AlanBernard and Munchy2007 like this.