APCarrier.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("Advanced Platformer 2D/Carrier")]
  4. public class APCarrier : BaseBehaviour
  5. {
  6. public Vector2 ComputeVelocityAt(Vector2 worldPoint)
  7. {
  8. Vector3 rhs = (Vector3)worldPoint - base.transform.position;
  9. return this.m_linearVel + (Vector2)Vector3.Cross(this.m_angularVel, rhs);
  10. }
  11. private void Start()
  12. {
  13. this.m_prevPos = base.transform.position;
  14. this.m_prevRot = base.transform.rotation;
  15. this.m_linearVel = Vector2.zero;
  16. this.m_angularVel = Vector3.zero;
  17. this.m_bNextUpdate = false;
  18. this.m_bUpdate = false;
  19. }
  20. private void FixedUpdate()
  21. {
  22. if (!this.m_animationMode)
  23. {
  24. this.UpdateVelocities();
  25. }
  26. else
  27. {
  28. if (this.m_bNextUpdate)
  29. {
  30. this.m_bNextUpdate = false;
  31. base.transform.position = this.m_nextPos;
  32. base.transform.rotation = this.m_nextRot;
  33. }
  34. this.UpdateVelocities();
  35. this.m_bUpdate = true;
  36. }
  37. }
  38. private void Update()
  39. {
  40. if (this.m_animationMode && this.m_bUpdate)
  41. {
  42. this.m_bUpdate = false;
  43. this.m_bNextUpdate = true;
  44. this.m_nextPos = base.transform.position;
  45. this.m_nextRot = base.transform.rotation;
  46. base.transform.position = this.m_prevPos;
  47. base.transform.rotation = this.m_prevRot;
  48. }
  49. }
  50. private void UpdateVelocities()
  51. {
  52. this.m_linearVel = ((Vector2)base.transform.position - this.m_prevPos) / Time.fixedDeltaTime;
  53. float num = Quaternion.Angle(this.m_prevRot, base.transform.rotation);
  54. Vector3 lhs = this.m_prevRot * Vector3.up;
  55. Vector3 rhs = base.transform.rotation * Vector3.up;
  56. if (Vector3.Cross(lhs, rhs).z < 0f)
  57. {
  58. num = -num;
  59. }
  60. this.m_angularVel.z = 0.0174532924f * num / Time.fixedDeltaTime;
  61. this.m_prevPos = base.transform.position;
  62. this.m_prevRot = base.transform.rotation;
  63. }
  64. public bool m_animationMode = true;
  65. private bool m_bNextUpdate;
  66. private bool m_bUpdate;
  67. private Vector2 m_prevPos;
  68. private Quaternion m_prevRot;
  69. private Vector2 m_nextPos;
  70. private Quaternion m_nextRot;
  71. private Vector2 m_linearVel;
  72. private Vector3 m_angularVel;
  73. }