using System; 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.6f; // 移动时间 // 跳跃相关 private const float HighJumpSpeed = 2.4f; // 高跳速度 private const float LowJumpSpeed = 2f; // 低跳速度 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 ? HighJumpSpeed : LowJumpSpeed; 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; } } }