文章

使用Character Controller進行角色移動

使用 Unity 的 Character Controller 組件來實現角色的移動,是製作 3D 遊戲角色的一種常見方法。Character Controller 提供了簡單而強大的移動處理能力,例如碰撞檢測、簡化的物理計算等,適合於各種類型的遊戲角色控制。

1. 添加 Character Controller 組件

步驟 1:選擇角色物件

  1. 選擇角色物件
    • Hierarchy 視窗中選擇你的角色物件(例如,Player)。
  2. 添加 Character Controller
    • Inspector 視窗中,點擊 Add Component 按鈕,搜索 Character Controller,然後將其添加到角色物件上。
  3. 調整 Character Controller 屬性
    • 根據角色模型的大小調整 Character ControllerCenterHeight 屬性,以覆蓋角色的形狀。
    • Radius:設置角色的寬度。
    • Height:設置角色的高度。
    • Step Offset:控制角色能夠跨越的小高度,例如台階。

2. 編寫角色移動腳本

步驟 1:創建角色移動腳本

  1. 創建新腳本
    • Assets 資料夾中右鍵選擇 Create > C# Script,命名為 PlayerMovement
  2. 編寫移動邏輯
    • 編輯 PlayerMovement 腳本來控制角色的移動,使用 Character Controller 進行碰撞檢測和移動。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    
    using UnityEngine;
    
    [RequireComponent(typeof(CharacterController))]
    public class PlayerMovement : MonoBehaviour
    {
        public float speed = 6.0f;      // 移動速度
        public float gravity = -9.81f;  // 重力值
        public float jumpHeight = 1.5f; // 跳躍高度
    
        private CharacterController controller;
        private Vector3 velocity;       // 垂直速度(重力和跳躍)
        private bool isGrounded;        // 檢測角色是否在地面上
    
        void Start()
        {
            // 獲取 Character Controller 組件
            controller = GetComponent<CharacterController>();
        }
    
        void Update()
        {
            // 檢測是否在地面上
            isGrounded = controller.isGrounded;
    
            if (isGrounded && velocity.y < 0)
            {
                velocity.y = -2f;  // 重置垂直速度,稍微向下推以保持接地狀態
            }
    
            // 獲取水平移動的輸入
            float moveX = Input.GetAxis("Horizontal");
            float moveZ = Input.GetAxis("Vertical");
    
            // 計算移動方向
            Vector3 move = transform.right * moveX + transform.forward * moveZ;
    
            // 移動角色
            controller.Move(move * speed * Time.deltaTime);
    
            // 檢測跳躍輸入
            if (Input.GetButtonDown("Jump") && isGrounded)
            {
                velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); // 計算跳躍速度
            }
    
            // 應用重力
            velocity.y += gravity * Time.deltaTime;
    
            // 移動角色垂直方向(重力影響)
            controller.Move(velocity * Time.deltaTime);
        }
    }
    

步驟 2:將腳本附加到角色

  1. 附加腳本
    • PlayerMovement 腳本拖放到 Player 物件上。

3. 配置角色控制屬性

  1. 調整角色速度和跳躍屬性
    • Inspector 視窗中選中 Player 物件,調整 PlayerMovement 腳本的 SpeedJump HeightGravity 等參數。
  2. 測試角色控制
    • 點擊 Play 按鈕,使用鍵盤的箭頭鍵或 WASD 鍵移動角色,使用 Space 鍵進行跳躍。
    • 確保角色能夠正常移動和跳躍,並正確地受重力影響。

4. 添加角色旋轉控制(可選)

步驟 1:在腳本中添加旋轉邏輯

  1. 修改 PlayerMovement 腳本

    為了讓角色根據鼠標移動進行旋轉,添加旋轉控制邏輯。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    
    using UnityEngine;
    
    [RequireComponent(typeof(CharacterController))]
    public class PlayerMovement : MonoBehaviour
    {
        public float speed = 6.0f;
        public float gravity = -9.81f;
        public float jumpHeight = 1.5f;
        public float mouseSensitivity = 100f; // 鼠標靈敏度
    
        private CharacterController controller;
        private Vector3 velocity;
        private bool isGrounded;
    
        private float xRotation = 0f;
    
        void Start()
        {
            controller = GetComponent<CharacterController>();
            Cursor.lockState = CursorLockMode.Locked; // 鎖定鼠標光標
        }
    
        void Update()
        {
            isGrounded = controller.isGrounded;
    
            if (isGrounded && velocity.y < 0)
            {
                velocity.y = -2f;
            }
    
            // 水平移動
            float moveX = Input.GetAxis("Horizontal");
            float moveZ = Input.GetAxis("Vertical");
            Vector3 move = transform.right * moveX + transform.forward * moveZ;
            controller.Move(move * speed * Time.deltaTime);
    
            // 跳躍
            if (Input.GetButtonDown("Jump") && isGrounded)
            {
                velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
            }
    
            // 重力影響
            velocity.y += gravity * Time.deltaTime;
            controller.Move(velocity * Time.deltaTime);
    
            // 角色旋轉
            float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
            xRotation -= mouseX;
            xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    
            transform.Rotate(Vector3.up * mouseX);
        }
    }
    
  2. 測試旋轉控制

    • 點擊 Play,使用滑鼠移動控制角色視角方向。

5. 測試和調整角色控制

  1. 播放測試
    • 測試角色的移動、跳躍和旋轉,確保控制流暢,角色能夠正常與場景中的其他物件互動。
  2. 調整參數
    • 根據測試結果,調整 SpeedJump HeightMouse Sensitivity 等屬性,以獲得更好的遊戲體驗。

6. 小結

通過使用 Character Controller 和基本的 C# 腳本,你可以快速創建一個可移動的3D角色。這種方法簡單易用,適合用於大多數3D遊戲中的角色控制。隨著需求的變化,可以進一步擴展腳本功能,例如添加更多動作、複雜的動畫控制或自定義的物理效果。

本文章以 CC BY 4.0 授權