After instantiation, camera no longer follows anything

Options
My game is "supposed" to work kind of like slither.io. Simple mouse based controls, rotating character, non-rotating camera.

My camera used to work fine with just one player, but now that I am instantiating it through PhotonNetwork my camera is totally unresponsive. This is how I instatiate my player character.

PhotonNetwork.Instantiate(playerPrefab.name, new Vector2(0, 0), Quaternion.identity, 0);

And this is how my camera used to work

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class CameraControls : MonoBehaviour
{
    public GameObject player;
    private Vector3 offset;
    void Start()
    {
        offset = transform.position - player.transform.position;
        Debug.Log("Camera Follow Start");
    }
    void LateUpdate()
    {
        transform.position = player.transform.position + offset;
    }
}
I have tried several pre-baked solutions and have had no results getting the camera to follow the player anymore. Because my player rotates while my camera does not I cannot simply make the camera a child of the player. I have had the camera and the player a child of an object, and had them separate. As soon as I remove the Photon stuff, the camera works fine.

Would love some ideas, help, a tutorial, video, anything to give me a direction to what I can do to figure this out.

Comments

  • develax
    Options
    When you instantiate a player via PhotonNetwork your prefab object

    public GameObject player;

    would be null and you would get an error in the editor console.

    I guess it works only when you put your Player on the scene before running the game. If I'm right it wouldn't work even if you instantiate your Player via the Unity Instantiate standard method.

    I would recommend going through this tutorial to understand some differences with multiplayer games before you start working on your own. Or at least get acquainted with their version of the code for the camera.

  • jfraser
    jfraser
    edited July 2019
    Options
    Thanks for the reply @devlax. No error in the console. It ended up being a problem with the way I had the prefab setup. I ended up making the camera a child of the player object and just disabling rotation through a separate script. Probably not the most eloquent solution, but everything works now for as many characters as I want to join.

    Your tutorial and links sent me down the rabbit hole to this solution, so thank you very much.

    Thanks again!