UNET Clients Laggy
This Game im working on is a sort of multiplayer snake game. The script below basically spawns in tails after the player has eaten a food and then moves it with the player. the problem is on the server host the tails and player move perfectly while the clients are laggy. On clients the tails are shooting everywhere and they arent smoothly following behind the client as they should. (The Die Function hasnt been implemented)
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine.Networking;
public class MovementController : NetworkBehaviour
{
public float speed = 0.5f;
public float poopSpeed = 0.5f;
public GameObject tailPrefab;
Vector3 dir = Vector3.forward;
public List<Transform> tail = new List<Transform>();
public bool eating = false;
public bool digested = false;
public float movementCooldownTime = 0.15f;
bool canTurn = true;
bool canTurnRight;
bool canTurnLeft;
public GameObject poopParticles;
public GameObject deathParticles;
public GameObject tailRoot;
public GameObject head;
public Transform dirUp;
public Transform dirDown;
public Transform dirLeft;
public Transform dirRight;
public PlayerCamera cam;
public SoundController sound;
public GameObject eatingParticles;
public bool isDead = false;
private Vector3 vecc;
void Start ()
{
InvokeRepeating ("Move", speed, speed);
if (isLocalPlayer) {
canTurnRight = true;
canTurnLeft = true;
sound = GetComponent<SoundController> ();
}
}
void Update ()
{
if (isLocalPlayer) {
if (Input.GetKey (KeyCode.W) && canTurn) {
head.transform.rotation = dirUp.transform.rotation;
StartCoroutine (TurnCoolDown (movementCooldownTime));
dir = Vector3.forward;
canTurnRight = true;
canTurnLeft = true;
}
if (Input.GetKey (KeyCode.A) && canTurnLeft && canTurn) {
head.transform.rotation = dirLeft.transform.rotation;
StartCoroutine (TurnCoolDown (movementCooldownTime));
dir = Vector3.left;
canTurnRight = false;
canTurnLeft = true;
}
if (Input.GetKey (KeyCode.S) && canTurn) {
head.transform.rotation = dirDown.transform.rotation;
StartCoroutine (TurnCoolDown (movementCooldownTime));
dir = Vector3.back;
canTurnRight = true;
canTurnLeft = true;
}
if (Input.GetKey (KeyCode.D) && canTurnRight && canTurn) {
head.transform.rotation = dirRight.transform.rotation;
StartCoroutine (TurnCoolDown (movementCooldownTime));
dir = Vector3.right;
canTurnRight = true;
canTurnLeft = false;
}
}
}
void Move ()
{
Vector3 vec = transform.position;
vecc = vec;
transform.Translate (dir);
if (tail.Count > 0)
{
tail.Last ().position = vec;
tail.Insert (0, tail.Last ());
tail.RemoveAt (tail.Count - 1);
}
if (eating && digested)
{
CmdSpawnTail ();
eating = false;
digested = false;
}
sound.EmitSound (0,0,0.2f,false);
}
public void TriggerEating()
{
StartCoroutine (Eat (poopSpeed));
}
void OnTriggerEnter(Collider col)
{
if (col.tag =="Food")
{
TriggerEating ();
col.GetComponent<Food> ().spawner.GetComponent<FoodSpawner> ().enabled = true;
col.GetComponent<Food> ().spawner.GetComponent<FoodSpawner> ().start = true;
sound.EmitSound (1,1,0.05f,false);
Instantiate (eatingParticles, col.transform.position, col.transform.rotation);
Destroy (col.gameObject);
}
if (col.tag =="Border" || col.tag =="Obstacle")
{
Die ();
}
if (col.tag =="Tail")
{
Die ();
}
}
IEnumerator TurnCoolDown(float cooldown)
{
canTurn = false;
yield return new WaitForSeconds (cooldown);
canTurn = true;
}
IEnumerator Col(GameObject obj)
{
obj.GetComponent<BoxCollider> ().isTrigger = false;
yield return new WaitForSeconds (0.1f);
obj.GetComponent<BoxCollider> ().isTrigger = true;
}
void Die()
{
if (isLocalPlayer) {
Instantiate (deathParticles, head.transform.position, head.transform.rotation);
Destroy (GetComponent<PlayerInfo> ().ui.gameObject);
Destroy (this.gameObject);
}
}
void Poop()
{
if (tail.Count > 0)
{
GameObject go = Instantiate (poopParticles, tail.Last ().position, tail.Last ().rotation) as GameObject;
NetworkServer.Spawn (go);
}
}
[Command]
void CmdSpawnTail()
{
GameObject go = (GameObject)Instantiate (tailPrefab , vecc, Quaternion.identity);
go.GetComponent<Tail> ().root = gameObject;
tail.Insert (0, go.transform);
StartCoroutine (Col (go));
NetworkServer.Spawn(go);
}
IEnumerator Eat(float eatTime)
{
eating = true;
yield return new WaitForSeconds (eatTime);
digested = true;
Poop ();
}
}
Your answer
Follow this Question
Related Questions
UNET (Multiplayer) calling [command] function twice 0 Answers
[UNet] Network Animator transition stutters 0 Answers
UNet - Is there a way to get clients and their assigned gameobjects? 0 Answers
Designing a multiplayer lobby for LAN 1 Answer
Locally disabling GameObjects with network transform components attached. 1 Answer