- Home /
When i make a multiplayer game, all characters are controlled by everyone
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;
}
}
}
Answer by fherbst · Feb 03, 2014 at 05:27 PM
For your player, you need something like
if (networkView.isMine) {
// do all the input handling
}
(you probably have a NetworkView on the prefab for the transform synchronisation)
I get an error that says - An instance of type 'Unity.NetworkView' is required to access non static member 'is$$anonymous$$ine'.
what do i have to do to fix that? (i'm new to progra$$anonymous$$g so i don't know much)
I tried adding - var UnityEngine.NetworkView; but it just gave me another error saying Unespected token: ..
idk what to do ;-;