LagPosition.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using UnityEngine;
  3. public class LagPosition : MonoBehaviour
  4. {
  5. public void OnRepositionEnd()
  6. {
  7. this.Interpolate(1000f);
  8. }
  9. private void Interpolate(float delta)
  10. {
  11. Transform parent = this.mTrans.parent;
  12. if (parent != null)
  13. {
  14. Vector3 vector = parent.position + parent.rotation * this.mRelative;
  15. this.mAbsolute.x = Mathf.Lerp(this.mAbsolute.x, vector.x, Mathf.Clamp01(delta * this.speed.x));
  16. this.mAbsolute.y = Mathf.Lerp(this.mAbsolute.y, vector.y, Mathf.Clamp01(delta * this.speed.y));
  17. this.mAbsolute.z = Mathf.Lerp(this.mAbsolute.z, vector.z, Mathf.Clamp01(delta * this.speed.z));
  18. this.mTrans.position = this.mAbsolute;
  19. }
  20. }
  21. private void Awake()
  22. {
  23. this.mTrans = base.transform;
  24. }
  25. private void OnEnable()
  26. {
  27. if (this.mStarted)
  28. {
  29. this.ResetPosition();
  30. }
  31. }
  32. private void Start()
  33. {
  34. this.mStarted = true;
  35. this.ResetPosition();
  36. }
  37. public void ResetPosition()
  38. {
  39. this.mAbsolute = this.mTrans.position;
  40. this.mRelative = this.mTrans.localPosition;
  41. }
  42. private void Update()
  43. {
  44. this.Interpolate((!this.ignoreTimeScale) ? Time.deltaTime : RealTime.deltaTime);
  45. }
  46. public Vector3 speed = new Vector3(10f, 10f, 10f);
  47. public bool ignoreTimeScale;
  48. private Transform mTrans;
  49. private Vector3 mRelative;
  50. private Vector3 mAbsolute;
  51. private bool mStarted;
  52. }