- Home /
When i connect to my server the host can't move just the client can move.
pls help the host can't move and watch around here is my script: using UnityEngine; using gui = UnityEngine.GUILayout;
public class GameMenu : MonoBehaviour
{
public GameObject PlayerPrefab;
string ip = "192.168.1.5";// ip : 127.0.0.1
static string playername = "Guest";
public void CreatePlayer()
{
connected = true;
var pl = (GameObject)Network.Instantiate(PlayerPrefab, transform.position, transform.rotation, 1);
pl.camera.enabled = true;
transform.camera.enabled = false;
}
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);
playername = gui.TextField(playername);
if (gui.Button("^2Connect to Match"))
{
Network.Connect(ip, 5300);
}
if (gui.Button("^1Host a Match"))
{
Network.InitializeServer(10, 5300, false);
}
if (gui.Button("^3Single match"))
{
Application.LoadLevel ("Single");
}
}
}
}
Answer by Mahtrok · Jun 03, 2013 at 04:32 PM
I don't think the Error is inside your Network setup but in your move script. Did you deactivate both move & Camera for NetworkPlayers that aren't the active ones? Try Adding
// c#
void Start () {
if (!Network.isMine)
this.enabled = false;
}
// JavaScript
function Start () {
if (!Network.isMine)
this.enabled = false;
}
to both scripts. If you allready did that it would be nice to see your movement script.
Greetz
my player prefab hasn't got any script. When the player connect to the server the server just create a player prefab without any script. So i dont use just this script. :D
Does he have a CharacterController or how do you move it without any script?
To deactivate Cameras and Controllers that aren't the ones the actual client ( or server ) should be able to Access Create two simple scripts, the first one has to be attached to the Camera
Camera: using UnityEngine; using System.Collections;
public class netCamDeactivator {
void Start () {
// If is not my Player
if(!Network.is$$anonymous$$ine) {
// If there is an AudioListener on your Camera deactivate it
GetComponent<AudioListener>().enabled = false;
// Deactivate the Camera
GetComponent<Camera>().enabled = false;
}
}
The second one should be attached to your Player
using UnityEngine;
using System.Collections;
public class NetPlayerDeactivator {
void Start () {
// deactivate the CharacterController that is not $$anonymous$$ine in the Network
if (!Network.is$$anonymous$$ine) {
GetComponent<CharacterController>().enabled = false;
}
}
}
be sure that both player and Camera have a NetworkView, if the Camera is a child Object of the player you may use
void Start () {
// deactivate the CharacterController that is not $$anonymous$$ine in the Network
if (!Network.is$$anonymous$$ine) {
GetComponent<CharacterController>().enabled = false;
GetComponentInChildren<AudioListener>().enabled = false;
GetComponentInChildren<Camera>().enabled = false;
}
}
then the Camera doesnt have to have a networkView for the player has one. Like this you can activate or deactivate any script or Controller attached to the clients player and ensure that only the one that is the current client may be controlled. For Network is creating local instances of the player on all connected clients and the server, all scripts and Controllers attached to the players Prefab are instantiated as well and in worst case you are moving not only your player around but all connected and Network.Instantiated players by Pressling a key through your Input. Same is for the camera, you will only See the rendered Scene of one of the Cameras on your Screen while all Others are not displayed, so if you deactivate These Cameras not belonging to your current client only the one attached to your player will be displayed.
Ok thanks i'll see that tomorow. Now here the time is 22:52 :D
in the second script i got these errors: Assets/Scripts/NetPlayerDeactivator.cs(8,24): error CS0117: UnityEngine.Network' does not contain a definition for
is$$anonymous$$ine'
Assets/Scripts/NetPlayerDeactivator.cs(9,16): error CS0103: The name `GetComponent' does not exist in the current context
the problem was with the networkview it was in unrealible but now the host and the client see different position of the object and everybody move the all players. I know what do you want with that script so i'll try write a script like your
Answer by Rushabhshroff98 · Dec 01, 2015 at 10:27 AM
Use this
if(!GetComponent.().isMine){
GetComponent.<CharacterController>().enabled = false;
GetComponentInChildren.<AudioListener>().enabled = false;
GetComponentInChildren.<Camera>().enabled = false;
}
Your answer
Follow this Question
Related Questions
Networking sending info only one direction 0 Answers
Multiplayer problem 1 Answer
Multiplayer syncronisation 1 Answer
Stop host without kick clients 1 Answer
How to get the Host's player position ? 0 Answers