Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach the camera to a player object instantiated by HLAPI Network Manager?

Tags:

c#

unity3d

In short, I have a very simple multiplayer game. It's the Roll A Ball game (Unity3D tutorial). So right now I have the players etc spawning perfectly and everyone is able to control their own balls perfectly fine.

But here's the problem: I've got a default Main Camera. Since it's only the local player itself that needs to see it, I figured there's no point in trying to spawn a seperate camera for each player on the server.

However, to make the camera follow the player, I need to attach it the player gameobject. Obviously I can't attach it to the player prefab as it's a clone the camera needs to follow. But since the player is being spawned by the Network Manager component, I have no idea on how to refer to this clone.

What I've tried myself:

public class CameraController : NetworkManager
{

    public GameObject playerPrefab;
    public Transform target;

    private Vector3 offset;

    public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    {
        GameObject player = (GameObject)Instantiate(playerPrefab, new Vector3(0, 0.5f, 0), Quaternion.identity);
        target = player.transform;
        NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    }

    void Start()
    {
        offset = transform.position - target.position;
    }

    void LateUpdate()
    {
        transform.position = transform.position + offset;
    }
}

But this resulted in: Screenshot of Error

Which I find extremely odd since as you can clearly see, there's no NetworkIdentity component on the NetworkManager object. I've been trying A LOT of things for the past 4 hours now and I just can't do it. So now I'm hoping you guys can help me out.

Edit: This is how the Network Manager normally spawns a player. As you can see, there's no code for it:

Network manager player spawn

like image 740
icecub Avatar asked Mar 28 '16 12:03

icecub


People also ask

How do you attach a camera to a networked object?

Hopefully, someone familiar with Mirror will chip in and help. It is a bad idea to make your camera part of a networked object's prefab. Just attach your camera to the object which represents that client after it is spawned in, and use an empty GameObject on the prefab as a mount point for the camera.

How to add script to main camera in Unity?

If you were created script from Project window then drag and drop the script on Main Camera. It will add the script to main Camera. Now, Drag and Drop the Player into Player field inside on CameraController script inside the Main Camera Inspector window.

Is it possible to instantiate player-prefabs without a camera?

This is a way to do it without a camera attached to the prefabs. I'm using a NetworkManager to instantiate Player-prefabs. (Same as you) I solved the problem of finding references to the clone objects by letting the clones tell the camera, who they are (or which transform belongs to them). The Player has the following script:

How to enable camera to follow player around the play field?

And, we’ll enable the camera to follow the player around the play field by writing a simple C# script. Create a script for Camera. click on Main Camera –> go to Inspector window –> click on Add components –>new Script –>Name the script (CameraController).


1 Answers

I had the same issue and figured out the followig solution. Seems like you already got a solution, but maybe it is interesting to share some possible ways for other people in the same situation.

This is a way to do it without a camera attached to the prefabs.

I'm using a NetworkManager to instantiate Player-prefabs. (Same as you)

I solved the problem of finding references to the clone objects by letting the clones tell the camera, who they are (or which transform belongs to them).

The Player has the following script:

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

public class PlayerController : NetworkBehaviour {



public override void OnStartLocalPlayer()
{
    GetComponent<MeshRenderer>().material.color = Color.blue;
    Camera.main.GetComponent<CameraFollow>().target=transform; //Fix camera on "me"

}

void Update ()
{
    if (!isLocalPlayer) 
    {
        return;
    }
    var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
    var z = Input.GetAxis ("Vertical") * Time.deltaTime * 3.0f;

    transform.Rotate (0, x, 0);
    transform.Translate (0,0,z);

}
}

On my default main Camera (there is no camera attached to the player prefab, just the default camera) I put the following script on. It takes a target which I initialised with the prefab using the inspector.

using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour {

public Transform target; //what to follow
public float smoothing = 5f; //camera speed

Vector3 offset;

void Start()
{
    offset = transform.position - target.position;
}

void FixedUpdate()
{
    Vector3 targetCamPos = target.position + offset;
    transform.position = Vector3.Lerp (transform.position,  targetCamPos,smoothing * Time.deltaTime);
}
}

After starting the game, each clone tells the camera who he is, so the target changes to the individual clients clone with this line from the Player's Script:

Camera.main.GetComponent<CameraFollow>().target=transform; //Fix camera on "me"

This way you don't need to create one camera per instance of player-prefabs (I'm not sure if this makes big differences in performance) and you don't have to deactivate all cameras which don't belong to your client.

If you host the game in the editor you can see that there is just 1 camera instead of one camera per connected client (like when you attach it to the prefab).

I think this is a good use of this method, you can use it to put things in it, which should be applied to the Local Player only.

public override void OnStartLocalPlayer()
{ 
}

I tried by starting the game in the editor and in a build and it seems to work well.

like image 92
Loahrs Avatar answered Oct 02 '22 21:10

Loahrs