>> >> >> Reference << << << <<<<<<Ref>>>>>>
>> >> >> Indexer << << << <<<<<<Idx>>>>>>
Matched: 0

Tags

    Categories

      Types

        Top Results

          Character Controller
          M: 2025-06-01 - ljf12825

          Character Controller是专为角色移动设计的物理组件,适合用于第一人称、第三人称角色控制,尤其适合需要“脚贴地”“走坡不滑”的场景
          它和Rigidbody不同,不依赖物理引擎施加力,而是手动控制角色移动的逻辑,更稳定、精准、游戏性更强

          Character Controller组件概览

          它本质是一个内置Capsule Collider + 内部碰撞处理器,支持走地、上坡、碰撞、阻挡等功能

          CharacterControllerPanel

          主要属性

          属性描述
          Center控制胶囊体中心位置
          Radius胶囊体的半径
          Height胶囊体高度
          Slope Limit可行走的最大坡度角(超过会滑下来)
          Step Offset可“跨越”的台阶高度
          Skin Width贴地/贴墙容差,过小会穿透,过大会卡住
          Min Move Distance小于这个值的移动会被忽略

          UnityManual CharacterController

          常用方法

          Move(Vector3 motion)
          移动角色,内部会自动处理碰撞,返回碰撞信息

          controller.Move(Vector3 motion * Time.deltaTime);
          

          支持斜坡、台阶检测、滑动、墙体推开等逻辑

          SimpleMobe(Vector3 motion)
          简化版移动,自动应用重力,不需要乘以Time.deltaTime

          controller.SimpleMove(new Vector3(x, 0, z));
          

          适合简单控制,不建议用于精细角色控制

          示例

          public class PlayerMove : MonoBehaviour
          {
              public float speed = 5f;
              public float gravity = -9.81f;
              public float jumpHeight = 1.5f;
          
              private CharacterController controller;
              private Vector3 velocity;
              private bool isGrounded;
          
              public Transform groundCheck;
              public float groundDistance = 0.4f;
              public LayerMask groundMask;
          
              void Start() => controller = GetComponent<CharacterController>();
          
              voie Update()
              {
                  isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
          
                  if (isGrounded && velocity.y < 0) velocity.y = -2f;
          
                  float x = Input.GetAxis("Horizontal");
                  float z = Input.GetAxis("Vertical");
          
                  Vector3 move = transform.right * x + transform.forward * z;
          
                  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);
              }
          }
          

          ChracterController vs Rigidbody

          特性CharacterControllerRigidbody
          物理碰撞响应自动响应更精确
          控制移动方式手动调用 Move通过力或 velocity
          支持旋转需要手动转动 transform支持
          上坡台阶检测自动处理需要额外实现
          重力需手动实现内建

          API

          UnityScripting CharacterController