CockMoveComp.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using Game;
  3. using UnityEngine;
  4. namespace Comp
  5. {
  6. public class CockMoveComp : MonoBehaviour
  7. {
  8. // 刚体
  9. private Rigidbody _rigidbody;
  10. // 水平运动相关
  11. private bool _isMoving; // 是否进行水平运动
  12. private float _movingTime; // 水平运动时间
  13. private const float MovingSpeed = 1f; // 移动速度
  14. private const float MovingTime = 0.4f; // 移动时间
  15. public int cockId;
  16. private Vector3 CreateMovingVector(float speed)
  17. {
  18. var forward = transform.forward;
  19. return new Vector3(speed * forward.x, 0, 0);
  20. }
  21. private Vector3 CreateJumpingVector(float y)
  22. {
  23. return Vector3.up * y;
  24. }
  25. public void Move()
  26. {
  27. _isMoving = true;
  28. _movingTime = MovingTime;
  29. }
  30. public void MoveAndNeverStop()
  31. {
  32. _isMoving = true;
  33. _movingTime = float.MaxValue;
  34. }
  35. public void Stop()
  36. {
  37. _isMoving = false;
  38. _movingTime = 0;
  39. }
  40. public void Jump(bool highJump)
  41. {
  42. var jumpSpeed = highJump ? AttackRandomUtil.GetHighJumpSpeed(cockId) : AttackRandomUtil.GetLowJumpSpeed(cockId);
  43. var jumpingVector = CreateJumpingVector(jumpSpeed);
  44. _rigidbody.AddForce(jumpingVector, ForceMode.Impulse); // 添加力
  45. }
  46. private void Start()
  47. {
  48. _rigidbody = GetComponent<Rigidbody>();
  49. }
  50. private void FixedUpdate()
  51. {
  52. if (!_isMoving) return;
  53. var movingVector = CreateMovingVector(MovingSpeed);
  54. transform.position += movingVector * Time.deltaTime;
  55. _movingTime -= Time.deltaTime;
  56. if (_movingTime <= 0)
  57. _isMoving = false;
  58. }
  59. }
  60. }