角色控制器 (Character Controller) 主要用于第三人称玩家控制或者是不使用__刚体__物理组件的第一人称玩家控制。
属性: | 功能: |
---|---|
Slope Limit | 将碰撞体限制为爬坡的斜率不超过指示值(以度为单位)。 |
Step Offset | 仅当角色比指示值更接近地面时,角色才会升高一个台阶。该值不应该大于角色控制器的高度,否则会产生错误。 |
Skin width | 两个碰撞体可以穿透彼此且穿透深度最多为皮肤宽度 (Skin Width)。较大的皮肤宽度可减少抖动。较小的皮肤宽度可能导致角色卡住。合理设置是将此值设为半径的 10%。 |
Min Move Distance | 如果角色试图移动到指示值以下,根本移动不了。此设置可以用来减少抖动。在大多数情况下,此值应保留为 0。 |
Center | 此设置将使胶囊碰撞体在世界空间中偏移,并且不会影响角色的枢转方式。 |
Radius | 胶囊碰撞体的半径长度。此值本质上是碰撞体的宽度。 |
Height | 角色的__胶囊碰撞体__高度。更改此设置将在正方向和负方向沿 Y 轴缩放碰撞体。 |
传统末日风格的第一人称控制在现实中并不真实。该角色每小时能跑 90 英里,可以立即停止并急转弯。因为该角色非常不真实,所以使用刚体和物理组件来创造这种行为有点不切实际,并会让玩家产生错觉。解决方案是使用专门的角色控制器。角色控制器只是一个胶囊形状的__碰撞体__,可以通过脚本来命令这个碰撞体向某个方向移动。然后,控制器将执行运动,但会受到碰撞的约束。控制器将沿着墙壁滑动,走上楼梯(如果低于 Step Offset 值),并走上 Slope Limit 设置范围内的斜坡。
控制器本身不会对力作出反应,也不会自动推开刚体。
如果要通过角色控制器来推动刚体或对象,可以编写脚本通过 OnControllerColliderHit() 函数对与控制器碰撞的任何对象施力。
另一方面,如果希望玩家角色受到物理组件的影响,那么可能更适合使用刚体,而不是角色控制器。
可以修改 Height 和 Radius 属性来适应角色的网格。对于人形角色,建议始终使用 2 米左右的值。如果轴心点并非刚好在角色的中心,还可以修改胶囊体的 Center 属性。
Step Offset 属性也可能有影响,对于身高 2 米的人,请确保此值在 0.1 到 0.4 之间。
Slope Limit 不应太小。通常,使用 90 度的值效果最佳。由于胶囊体形状的原因,角色控制器将无法爬墙。
要正确调整角色控制器,__Skin Width__ 属性是最重要的属性之一。 如果角色被卡住,那么很可能是因为 Skin Width 设置过小。Skin Width 允许对象轻微穿透控制器,但可消除抖动并防止被卡住。
最好是让 Skin Width 的值至少大于 0.01 并且比 Radius 的值大 10%。
建议将 Min Move Distance 保持为 0。
请参阅此处的角色控制器脚本参考
CharacterController
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.