- Home /
Missing line renderer in multiplayer?
hi i am doing a game like snake but it is multiplayer and i have this script to spawn a line renderer and multiply SphereCollider after the player that moves around in the game and it works great. i have tested some now and i believe if i slow down the movements of the player it seams to work? but i don't want my player to move in slow motion is there any specific to know about line renderer in multiplayer when using network view?
using UnityEngine;
using System.Collections;
public class drawLine : MonoBehaviour {
public LineRenderer lineRender;
private int numberOfPoints = 0;
private Vector3 moveDirection = Vector3.zero;
public float speed = 2.5F;
public float rotateSpeed = 2.5F;
public float maxDelay = 5.0F;
private float delay;
private Vector3 playerPos;
private bool firstTimeSpawnCollider;
private Vector3 spawnColliderPos;
private bool isAlive;
// Use this for initialization
void Start () {
delay = maxDelay;
firstTimeSpawnCollider = true;
isAlive = true;
InvokeRepeating("SpawnPlayerCollider", 0.7f, 0.2F);
}
// Update is called once per frame
void Update () {
CharacterController controller = GetComponent<CharacterController>();
if (isAlive) {
transform.Rotate (0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
Vector3 forward = transform.TransformDirection (Vector3.forward) * speed;
controller.SimpleMove (forward * speed);
//if( Input.GetKey( KeyCode.Mouse0 ) ) {
numberOfPoints++;
lineRender.SetVertexCount (numberOfPoints);
playerPos = gameObject.transform.localPosition;
lineRender.SetPosition (numberOfPoints - 1, playerPos);
}
}
void SpawnPlayerCollider() {
GameObject player1 = new GameObject( "Player1sCollider" );
if (firstTimeSpawnCollider) {
spawnColliderPos = playerPos;
spawnColliderPos.z = spawnColliderPos.z - 2;
} else {
}
player1.transform.position = spawnColliderPos;
player1.AddComponent("SphereCollider");
SphereCollider player1Collider = player1.GetComponent<SphereCollider>();
player1Collider.radius = 0.4f;
player1Collider.isTrigger = true;
firstTimeSpawnCollider = false;
spawnColliderPos = playerPos;
player1.tag = "worm";
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "worm" ||other.gameObject.tag == "wall") {
isAlive = false;
CancelInvoke();
}
}
}
But now when i playing in multiplayer so is the line renderer and i believe that SphereCollider is missing for one player? anyone that have any idea why this is happening?
here is the server/client script
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour {
private const string typeName = "emilswormgame";
private const string gameName = "RoomName";
private HostData[] hostList;
public GameObject playerPrefab;
public bool firstSpawn;
// Use this for initialization
void Start () {
firstSpawn = false;
}
// Update is called once per frame
void Update () {
}
private void StartServer()
{
Network.InitializeServer(4, 25000, !Network.HavePublicAddress());
MasterServer.RegisterHost(typeName, gameName);
}
private void RefreshHostList()
{
MasterServer.RequestHostList(typeName);
}
void OnMasterServerEvent(MasterServerEvent msEvent)
{
if (msEvent == MasterServerEvent.HostListReceived)
hostList = MasterServer.PollHostList();
}
private void JoinServer(HostData hostData)
{
Network.Connect(hostData);
}
void OnConnectedToServer()
{
Debug.Log("Server Joined");
SpawnPlayer ();
//firstSpawn = false;
}
void OnServerInitialized()
{
SpawnPlayer ();
Debug.Log("Server Initializied");
}
private void SpawnPlayer()
{
if (!GameObject.FindGameObjectWithTag("player1")) {
Network.Instantiate (playerPrefab, new Vector3 (0f, 4f, -24f), Quaternion.identity, 0);
firstSpawn = false;
} else {
Network.Instantiate (playerPrefab, new Vector3 (15f, 4f, -24f), Quaternion.identity, 0);
}
}
void OnGUI()
{
if (!Network.isClient && !Network.isServer)
{
if (GUI.Button(new Rect(100, 100, 250, 100), "Start Server"))
StartServer();
if (GUI.Button(new Rect(100, 250, 250, 100), "Refresh Hosts"))
RefreshHostList();
if (hostList != null)
{
for (int i = 0; i < hostList.Length; i++)
{
if (GUI.Button(new Rect(400, 100 + (110 * i), 300, 100), hostList[i].gameName))
JoinServer(hostList[i]);
}
}
}
}
}
and my playerprefab hase this components;
Mesh Renderer;
DrawLine script (linereanderer and spherecollider sript);
Line Renderer;
character controller;
Network view;
here you can see it
It seams that it works now when i wait to start the game after every one has joined. Any one that can confirm that it can be so?
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
NetworkViews on Rotating Objects Crash Clients 0 Answers
Networking RPC calls never sent to other clients 1 Answer
Multiplayer push object error. 0 Answers
Character Animation Lags in Multiplayer 2 Answers