2D Multiplayer Cameras

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:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class PlayerController : NetworkBehaviour
{

    public float speed;             //Floating point variable to store the player's movement speed.

    private Rigidbody2D rb2d;       //Store a reference to the Rigidbody2D component required to use 2D Physics.

    // Use this for initialization
    void Start()
    {
        //Get and store a reference to the Rigidbody2D component so that we can access it.
        rb2d = GetComponent<Rigidbody2D>();
    }
    void Update()
    {
        if (!isLocalPlayer)
        {
            return;
        }
    }
    //FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
    void FixedUpdate()
    {

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.position += Vector3.left * speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.position += Vector3.right * speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            transform.position += Vector3.up * speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.DownArrow))
        {
            transform.position += Vector3.down * speed * Time.deltaTime;
        }

    }
}

Camera Code:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;


public class CameraController : NetworkBehaviour
{

    public GameObject player;       //Public variable to store a reference to the player game object


    private Vector3 offset;         //Private variable to store the offset distance between the player and camera

    CameraController mycam;

    // Use this for initialization
    void Start()
    {
        //Calculate and store the offset value by getting the distance between the player's position and camera's position.
        offset = transform.position - player.transform.position;
    }

    // LateUpdate is called after Update each frame
    void LateUpdate()
    {
            // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
            transform.position = player.transform.position + offset;
    }
}

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!

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

1 Like

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.

2 Likes