UnityContrib / framework

Unity Contribution Framework
MIT License
8 stars 2 forks source link

NetworkSyncLocalScale component #3

Open robintheilade opened 9 years ago

robintheilade commented 9 years ago

Make code generic, optimized and commented.

using UnityEngine;
using UnityEngine.Networking;

namespace Game
{
    public class NetworkSyncLocalScale : NetworkBehaviour
    {
        [SyncVar]
        private Vector3 localScaleSynced;
        public float threshold = 0.1f;

        public override void OnStartServer()
        {
            base.OnStartServer();
            this.localScaleSynced = this.transform.localScale;
        }

        private void Update()
        {
            if (this.isServer)
            {
                var distance = Vector3.Distance(this.localScaleSynced, this.transform.localScale);
                if (distance > this.threshold)
                {
                    this.localScaleSynced = this.transform.localScale;
                }
            }
            else
            {
                this.transform.localScale = this.localScaleSynced;
            }
        }
    }
}