基礎設置
-
角色控制器選擇:
-
使用Character Controller組件或Rigidbody + Capsule Collider
-
推薦使用Character Controller以獲得更精確的運動控制
-
-
輸入系統:
-
使用Unity的新輸入系統(Input System Package)處理玩家輸入
-
滑鏟實現
public class SlideController : MonoBehaviour
{[Header("Slide Settings")]public float slideSpeed = 10f;public float slideDuration = 1f;public float slideCooldown = 0.5f;public float slideHeight = 0.5f;public float normalHeight = 2f;private CharacterController controller;private bool isSliding = false;private float slideTimer;private float cooldownTimer;void Start(){controller = GetComponent<CharacterController>();}void Update(){HandleSlide();}void HandleSlide(){if (cooldownTimer > 0){cooldownTimer -= Time.deltaTime;return;}if (Input.GetKeyDown(KeyCode.LeftControl) && !isSliding){StartSlide();}if (isSliding){slideTimer -= Time.deltaTime;if (slideTimer <= 0){EndSlide();}// 保持滑鏟速度Vector3 moveDirection = transform.forward * slideSpeed;controller.Move(moveDirection * Time.deltaTime);}}void StartSlide(){isSliding = true;slideTimer = slideDuration;controller.height = slideHeight;controller.center = new Vector3(0, slideHeight * 0.5f, 0);}void EndSlide(){isSliding = false;cooldownTimer = slideCooldown;controller.height = normalHeight;controller.center = new Vector3(0, normalHeight * 0.5f, 0);}
}
貼墻跑實現
public class WallRunController : MonoBehaviour
{[Header("Wall Run Settings")]public float wallRunSpeed = 8f;public float wallRunGravity = 2f;public float wallRunDuration = 2f;public float wallJumpForce = 10f;public LayerMask wallRunLayer;private CharacterController controller;private bool isWallRunning = false;private Vector3 wallNormal;private float wallRunTimer;void Start(){controller = GetComponent<CharacterController>();}void Update(){CheckWallRun();HandleWallRun();}void CheckWallRun(){if (isWallRunning) return;RaycastHit hit;if (Physics.Raycast(transform.position, transform.right, out hit, 1f, wallRunLayer)){StartWallRun(hit.normal, false);}else if (Physics.Raycast(transform.position, -transform.right, out hit, 1f, wallRunLayer)){StartWallRun(hit.normal, true);}}void StartWallRun(Vector3 normal, bool isLeftWall){isWallRunning = true;wallNormal = normal;wallRunTimer = wallRunDuration;// 調整角色朝向與墻面平行Vector3 cross = Vector3.Cross(normal, Vector3.up);transform.rotation = Quaternion.LookRotation(cross, normal);}void HandleWallRun(){if (!isWallRunning) return;wallRunTimer -= Time.deltaTime;// 沿墻面移動Vector3 moveDirection = transform.forward * wallRunSpeed;// 應用自定義重力moveDirection.y -= wallRunGravity * Time.deltaTime;controller.Move(moveDirection * Time.deltaTime);// 檢查是否應該結束貼墻跑if (wallRunTimer <= 0 || !Physics.Raycast(transform.position, wallNormal, 1f, wallRunLayer)){EndWallRun();}// 墻跳if (Input.GetButtonDown("Jump")){WallJump();}}void WallJump(){Vector3 jumpDirection = (wallNormal + Vector3.up).normalized;// 應用跳躍力...EndWallRun();}void EndWallRun(){isWallRunning = false;}
}
高級技巧
-
動畫混合:
-
使用Animator Controller混合不同動作的動畫
-
設置適當的過渡條件和混合樹
-
-
運動曲線:
-
使用AnimationCurve調整動作的速度變化,使過渡更自然
-
-
相機效果:
-
在特殊動作時添加相機震動、視野變化等效果
-
使用Cinemachine實現平滑的相機跟隨
-
-
物理材質:
-
為不同表面設置不同的物理材質,影響摩擦力和彈跳效果
-
-
粒子效果:
-
在滑鏟時添加灰塵粒子
-
在貼墻跑時添加墻面火花效果
-
優化建議
-
狀態機模式:
-
實現一個完整的狀態機管理系統,管理跑酷的各種狀態
-
例如:站立、奔跑、滑鏟、貼墻跑、跳躍等狀態
-
-
輸入緩沖:
-
實現輸入緩沖系統,使動作銜接更流暢
-
-
物理預測:
-
使用射線檢測預測即將到來的動作機會(如前方可滑鏟區域或可貼墻跑的表面)
-