I have a networked game with a ship that the player flies around. The ship is a prefab with a camera. When the app runs, the player can move the ship around and the camera moves with the prefab as expected. However, when another player joins the game, that new player takes control of the camera. Each player still has control of their ship and they can move independently, but the camera follows the most recently joined player ship.
I'm at a loss becuase the camera is the child of the player prefab, so I don't understand how another player can have control of the camera.
I've tried adding a network identity with local player identity, but not sure what else to try.
I would like the camera to follow the local player ship.
Please let me know if anything else would help diagnose this issue.
There isn't really any relevant code, but this code does set the initial camera to inactive so that the prefab camera becomes the active camera...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
public class move : NetworkBehaviour {
public float moveSpeed = 1f;
public float smoothTimeY;
public float smoothTimeX;
private GameObject joystickcont;
private Camera firstcamera;
public Camera shipcamera;
[SyncVar(hook = "OnSetScale")]
private Vector3 scale;
[Command]
public void CmdSetScale(Vector3 vec)
{
scale = vec;
}
private void OnSetScale(Vector3 vec)
{
transform.localScale = vec;
}
void Start () {
if (!isLocalPlayer)
{
return;
}
firstcamera = GameObject.FindWithTag("firstcamera").GetComponent<Camera>();
firstcamera.gameObject.SetActive(false);
shipcamera.gameObject.SetActive(true);
CmdSetScale(gameObject.transform.localScale);
gameObject.transform.localScale = new Vector3(0.05f, 0.05f, 1);
}
void Update () {
if (!isLocalPlayer)
{
return;
}
CmdSetScale(gameObject.transform.localScale);
Vector3 dir = Vector3.zero;
dir.x = Input.GetAxis("Horizontal");
dir.y = Input.GetAxis("Vertical");
joystick moveJoystick = joystickcont.GetComponent<joystick>();
if (moveJoystick.InputDirection != Vector3.zero)
{
dir.x = moveJoystick.InputDirection.x;
dir.y = moveJoystick.InputDirection.y;
}
var rb = gameObject.GetComponent<Rigidbody2D>();
if (dir == Vector3.zero)
{
}
else
{
float heading = Mathf.Atan2(dir.x, dir.y);
transform.rotation = Quaternion.Inverse(Quaternion.Euler(0f, 0f, heading * Mathf.Rad2Deg));
rb.velocity = new Vector3(dir.x * .2f, dir.y * .2f, 0);
}
}
}