BulletEventMoveProcessor.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using UnityEngine;
  2. using System.Collections;
  3. public class BulletEventMoveProcessor
  4. {
  5. private Transform mBullet;
  6. private Vector3 mVelocity = Vector3.zero;
  7. private Vector3 mAcceleration = Vector3.zero;
  8. private Vector3 mDestPos = Vector3.zero;
  9. private float bullet_move_speed = 10f;
  10. private float mLeftAccelerationTime = 0;
  11. private bool mTraceTarget = false;
  12. private bool mEnabled = false;
  13. public BulletEventMoveProcessor(Transform bullet, BulletMoveType moveType, float moveSpeed,Vector3 initVelocity,Vector3 acceleration,Vector3 destPos,float accelerationTime)
  14. {
  15. mBullet = bullet;
  16. bullet_move_speed = moveSpeed;
  17. mVelocity = initVelocity;
  18. mAcceleration = acceleration;
  19. mLeftAccelerationTime = accelerationTime;
  20. mDestPos = destPos;
  21. mBullet.LookAt(mDestPos);
  22. if (mLeftAccelerationTime <= 0)
  23. {
  24. mTraceTarget = true;
  25. }
  26. mEnabled = false;
  27. }
  28. public void Start()
  29. {
  30. mEnabled = true;
  31. }
  32. public void Stop()
  33. {
  34. mBullet = null;
  35. mEnabled = false;
  36. }
  37. public void Update(float deltaTime)
  38. {
  39. if (!mEnabled)
  40. return;
  41. Vector3 curPos = mBullet.position + mVelocity * deltaTime;
  42. mBullet.position = curPos;
  43. if (mLeftAccelerationTime > 0)
  44. {
  45. mVelocity += mAcceleration * deltaTime;
  46. mLeftAccelerationTime -= deltaTime;
  47. if (mLeftAccelerationTime <= 0)
  48. {
  49. mBullet.LookAt(mDestPos);
  50. float leftTime = (mDestPos.z - curPos.z) / mVelocity.z;
  51. mVelocity.x = (mDestPos.x - curPos.x) / leftTime;
  52. mVelocity.y = (mDestPos.y - curPos.y) / leftTime;
  53. mTraceTarget = true;
  54. }
  55. }
  56. if (mTraceTarget && !mDestPos.FEqual(mBullet.position, 1e-2f))
  57. mVelocity = (mDestPos - mBullet.position).normalized * mVelocity.magnitude;
  58. }
  59. }