- Home /
Question by
the_megajoey · Jun 08, 2017 at 10:36 AM ·
networkingspawning
Camera not spawning properly on network
I am trying to spawn a camera for my multiplayer game but the code doesn't work and I'm not sure why, when I run the game and host the server no camera is instantiated in the hierarchy.
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PlayerMovement : NetworkBehaviour {
private float moveSpeed;
public GameObject camPrefab;
private GameObject camObj;
private Camera cam;
private Rigidbody2D rb2d;
void Awake()
{
if (isLocalPlayer)
{
CmdSetUpCamera ();
}
rb2d = gameObject.GetComponent<Rigidbody2D> ();
moveSpeed = 100.0f;
}
void Update()
{
if (isLocalPlayer)
{
float horizontal = Input.GetAxis ("Horizontal") * moveSpeed;
float vertical = Input.GetAxis ("Vertical") * moveSpeed;
rb2d.AddForce (Vector2.right * horizontal);
rb2d.AddForce (Vector2.up * vertical);
Vector3 dir = Input.mousePosition - cam.WorldToScreenPoint (transform.position);
float angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
}
}
[Command]
void CmdSetUpCamera()
{
camObj = (GameObject)Instantiate (camPrefab, transform.position, transform.rotation);
NetworkServer.Spawn (camObj);
cam = camObj.GetComponent<Camera> ();
}
}
both the player prefab and the camera prefab have local player authority checked on their network identity.
Why is the camera not spawning in the hierarchy and how do I fix it?
Comment