MoveProcessor.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using UnityEngine;
  2. using UnityEditor;
  3. public class MoveProcessor
  4. {
  5. readonly Transform mCasterTrans;
  6. float mTime;
  7. float mAcceleration;
  8. float mInitSpeed;
  9. float mSpeed;
  10. float mCurrentTime;
  11. Vector3 mTargetPosition;
  12. bool mMoveEnd = true;
  13. public bool IsMoving
  14. {
  15. get { return !mMoveEnd; }
  16. }
  17. public bool IsMovingUp
  18. {
  19. get { return mTargetPosition.y - mCasterTrans.position.y > 0; }
  20. }
  21. public MoveProcessor(Transform caster)
  22. {
  23. mCasterTrans = caster;
  24. }
  25. public void Start(float initSpeed, float acceleration, Vector3 destPos)
  26. {
  27. Stop();
  28. mTargetPosition = destPos;
  29. mAcceleration = acceleration;
  30. mInitSpeed = initSpeed;
  31. mMoveEnd = false;
  32. float dist = Vector3.Distance(destPos, mCasterTrans.position);
  33. if (Mathf.Abs(acceleration) > 0)
  34. {
  35. float temp = initSpeed / Mathf.Abs(acceleration);
  36. mTime = Mathf.Sqrt(dist + temp * temp) - temp;
  37. }
  38. else
  39. {
  40. mTime = dist / initSpeed;
  41. }
  42. Debug.LogError("击退时间:" + mTime+" dist:"+dist);
  43. }
  44. public void Stop()
  45. {
  46. mCurrentTime = 0f;
  47. mMoveEnd = true;
  48. }
  49. public void Update(float deltaTime)
  50. {
  51. if (!mMoveEnd)
  52. {
  53. mSpeed = mInitSpeed + mCurrentTime * mAcceleration;
  54. Vector3 pos = Vector3.MoveTowards(mCasterTrans.position, mTargetPosition, mSpeed * deltaTime);
  55. mCasterTrans.position = pos;
  56. mCurrentTime += deltaTime;
  57. if (mCurrentTime >= mTime || (mCasterTrans.position.FEqual(mTargetPosition)))
  58. {
  59. Stop();
  60. return;
  61. }
  62. }
  63. }
  64. }