Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by DuperCorp_Blackorito · Jun 28, 2016 at 02:31 AM · c#unity 5networkingmultiplayermultiplayer-networking

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 ();
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

212 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges