How to connect the camera to a player object created by HLAPI Network Manager?

In short, I have a very simple multiplayer game. This is a Roll A Ball game (Unity3D tutorial). So right now I have players, etc., And they all are developing well, and everyone can perfectly control their balls.

But here is the problem: I have a default main camera. Since this is only a local player who needs to see him, I decided that it makes no sense to try to create a separate camera for each player on the server.

However, for the camera to follow the player, I need to attach it to the player’s game object. Obviously, I can’t attach it to the player’s assembly, since it has to clone the camera. But since the player is generated by the Network Manager component, I have no idea how to access this clone.

What I 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 led to: Error screenshot

Which I find extremely strange, since, as you can clearly see, there is no NetworkIdentity component in the NetworkManager. I have tried many things in the last 4 hours, and I just can't do it. So now I hope you guys can help me.

Edit: this way Network Manager usually creates a player. As you can see, there is no code for this:

Network Manager Player

+5
source share
4 answers

I would add a camera to the compilation and then write a player script as follows:

 using UnityEngine.Networking; public class Player : NetworkBehaviour { public Camera camera; void Awake() { if(!isLocalPlayer) { camera.enabled = false; } } } 
+3
source

I had the same problem and figured out how to do it. It looks like you already have a solution, but it may be interesting to share some possible ways for other people in the same situation.

This is a way to do this without a camera attached to the assembly files.

I use NetworkManager to instantiate Player-prefabs. (Same as you)

I solved the problem of finding references to clone objects, allowing clones to tell the camera who they are (or what transformation belongs to them).

The player has a 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); } } 

In my main main OS (there is no camera attached to the player assembly, only the default camera). I have included the following script. It accepts the target that I initialized with 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 the individual clients with this line using the script player:

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

This way, you don’t need to create one camera for one instance of play players (I’m not sure if this has big differences in performance), and you don’t need to deactivate all cameras that do not belong to your client.

If you place the game in the editor, you can see that on one connected client there is only one camera instead of one camera (for example, when you attach it to the assembly).

I think this is a good way to use this method, you can use it to put things in it that should only apply to the local player.

 public override void OnStartLocalPlayer() { } 

I tried to start the game in the editor and in the assembly, and it seems to work well.

+6
source

I really did not work with networks, but what if you create a local player

 Camera.main.transfor.SetParent(the transform of the local player here); 

As I understand it, every single instance of the game has a main camera.

+2
source

Thanks to Rafiwui point in the right direction, I finally managed to get it to work. All I needed to do was pretty good compared to its code. Final result:

 public Camera camera; void Awake() { camera.enabled = false; } public override void OnStartLocalPlayer() { camera.enabled = true; } 

Thank you for helping me! It was a whole day for me.

+2
source

Source: https://habr.com/ru/post/1245903/


All Articles