When i make a multiplayer game, all characters are controlled by everyone…
by this I mean that anyone who joins the game will be controlling everyone in the game. here is my script to join the game:
using UnityEngine;
using gui = UnityEngine.GUILayout;
public class GameMenu : MonoBehaviour
{
public GameObject PlayerPrefab;
string ip = "127.0.0.1";
public void CreatePlayer()
{
connected = true;
var g = (GameObject)Network.Instantiate(PlayerPrefab, transform.position, transform.rotation, 1);
g.camera.enabled = true;
camera.enabled = false;
AudioListener.volume = 0;
}
void OnDisconnectedFromServer()
{
connected = false;
}
void OnPlayerDisconnected(NetworkPlayer pl)
{
Network.DestroyPlayerObjects(pl);
}
void OnConnectedToServer()
{
CreatePlayer();
}
void OnServerInitialized()
{
CreatePlayer();
}
bool connected;
void OnGUI()
{
if (!connected)
{
ip = gui.TextField(ip);
if (gui.Button("connect"))
{
Network.Connect(ip, 5300);
}
if (gui.Button("host"))
{
Network.InitializeServer(10, 5300, false);
}
}
}
}
and this is my script to control the player:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
// Use this for initialization
void Start()
{
}
float x, y;
void Update()
{
if (Input.GetMouseButtonDown(1))
Screen.lockCursor = !Screen.lockCursor;
if (Screen.lockCursor)
{
x += Input.GetAxis("Mouse X") * 5;
y -= Input.GetAxis("Mouse Y") * 4;
var rotation = Quaternion.Euler(y, x, 0);
transform.rotation = rotation;
}
}
}