using System; using Game; using UnityEngine; namespace Comp { public class CockMoveComp : MonoBehaviour { // 刚体 private Rigidbody _rigidbody; // 水平运动相关 private bool _isMoving; // 是否进行水平运动 private float _movingTime; // 水平运动时间 private const float MovingSpeed = 0.5f; // 移动速度 private const float MovingTime = 0.4f; // 移动时间 public int cockId; private Vector3 CreateMovingVector(float speed) { var forward = transform.forward; return new Vector3(speed * forward.x, 0, 0); } private Vector3 CreateJumpingVector(float y) { return Vector3.up * y; } public void Move() { _isMoving = true; _movingTime = MovingTime; } public void MoveAndNeverStop() { _isMoving = true; _movingTime = float.MaxValue; } public void Stop() { _isMoving = false; _movingTime = 0; } public void Jump(bool highJump) { var jumpSpeed = highJump ? AttackRandomUtil.GetHighJumpSpeed(cockId) : AttackRandomUtil.GetLowJumpSpeed(cockId); var jumpingVector = CreateJumpingVector(jumpSpeed); _rigidbody.AddForce(jumpingVector, ForceMode.Impulse); // 添加力 } private void Start() { _rigidbody = GetComponent(); } private void FixedUpdate() { if (!_isMoving) return; var movingVector = CreateMovingVector(MovingSpeed); transform.position += movingVector * Time.deltaTime; _movingTime -= Time.deltaTime; if (_movingTime <= 0) _isMoving = false; } } }