Unity Character Controller 角色卡在物体边缘问题求助
角色控制器跳至物体边缘时卡住无法下落的问题
- 操控角色跳到物体边缘时,角色未按预期下落,直接卡住。排查发现卡住时
isGrounded值为false,排除该变量的问题。 - 角色碰撞体确实接触到最高的立方体,但无论调整Character Controller检视面板中的Skin Width或Min Move Distance参数,角色都无法落到下方的第二个立方体/矩形上。使用Unity内置Character Controller,未添加额外碰撞体。
public float groundDistance = 0.2f; public LayerMask groundMask; public bool isGrounded; public Transform groundCheck; void Update() { isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); //isGrounded = controller.isGrounded; //isGrounded = Physics.SphereCast(transform.position, 0.25f, Vector3.down, out hit, 1f) //isGrounded = Physics.Raycast(transform.position, Vector3.down, 2f); 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(jump * -2f * gravity); } velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); }
已尝试的解决方法均无效:
- 编写基础代码让角色未着地时下落(设置
velocity.y = -2f) - 尝试三种不同的
isGrounded实现方式(代码中已注释),但要么无效,要么导致角色移动异常 - 测试过胶囊碰撞体、圆柱碰撞体
- 多次调整Raycast/SphereCast的原点位置、投射距离等参数
- 尝试不同脚本,以及在Update中仅调用一次
controller.Move()方法
内容的提问来源于stack exchange,提问作者RelevantUni98
相关产品推荐
相关产品推荐

