SynchronizationObject.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class SynchronizationObject : MonoBehaviour
  5. {
  6. public SyncObjectType type;
  7. public bool isSync;
  8. public string uid;
  9. private SyncObject data;
  10. private float Speed = 3;
  11. private float framSpeed;
  12. private float dis;
  13. private float pross;
  14. private void Start()
  15. {
  16. MultiPlayerManager.Instance.OnSynObject += OnSyncObject;
  17. }
  18. public void InitData(SyncObject data)
  19. {
  20. if (data != null)
  21. return;
  22. this.data = data;
  23. }
  24. public void OnSyncObject(SyncObject data)
  25. {
  26. if (data.id == uid)
  27. this.data = data;
  28. }
  29. private void Update()
  30. {
  31. if(type == SyncObjectType.Player)
  32. if (data != null && transform.localPosition != data.pos)
  33. {
  34. framSpeed = Time.deltaTime * Speed;
  35. dis = Vector3.Distance(transform.localPosition, data.pos);
  36. pross = 0;
  37. if (framSpeed > dis)
  38. {
  39. transform.localPosition = data.pos;
  40. }
  41. else
  42. {
  43. pross = framSpeed / dis;
  44. transform.localPosition = Vector3.Lerp(transform.localPosition, data.pos, pross);
  45. }
  46. }
  47. }
  48. }