You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ursina框架FirstPersonController碰撞体扩展设置问题求助

Ursina FirstPersonController碰撞体扩展解决方案

Ursina内置的FirstPersonController默认自带胶囊碰撞体,可通过调整默认碰撞体参数或替换为自定义BoxCollider的方式,让碰撞范围匹配你挂载的角色精灵大小,以下是两种可行实现方式:

方案1:调整默认胶囊碰撞体参数(性能最优,适合类人型角色)

默认胶囊碰撞体支持直接修改3个核心参数适配尺寸:

  • radius:碰撞体半径,默认值0.5
  • height:碰撞体总高度,默认值2
  • y_offset:碰撞体Y轴偏移量,默认值0(从角色脚底开始计算)
    你只需要在初始化FirstPersonController时传入对应参数即可,参数可根据你的Player实际大小调整:
# 替换原cam = FirstPersonController()行
cam = FirstPersonController(
    radius=0.8,
    height=2.5,
    y_offset=0
)

方案2:替换为自定义BoxCollider(适配任意形状角色)

如果你的角色是立方体等非类人形状,直接替换默认碰撞体为BoxCollider,指定size和center匹配角色尺寸即可,修改后的完整代码如下:

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController

class Player(Entity):
    def __init__(self, parent):
        super().__init__(
            model = 'cube',
            color = color.red,
            parent = parent,
            # 自定义角色尺寸,后面碰撞体参数要和该值对应
            scale = (1,2,1),
            position = (0,1,0) # 调整位置让角色底部和控制器脚底对齐
        )
        
class Floor(Entity):
    def __init__(self):
        super().__init__(
            model = 'cube',
            scale = (40, 3, 40),
            color = color.rgb(23, 45, 105),
            position = (0, -20, 0),
            collider = 'box'
        )

app = Ursina()
window.fps_counter.enabled = False
window.fullscreen = True

cam = FirstPersonController()
# 自定义BoxCollider,size和center和上面Player的尺寸、位置对应
cam.collider = BoxCollider(cam, size=(1,2,1), center=(0,1,0))
player = Player(cam)

floor = Floor()

def update():
    if held_keys['escape']:
        application.quit()

Sky()
app.run()

注意事项

  • 碰撞体的尺寸和偏移量需要和你角色精灵的scale、position完全对应,否则会出现碰撞范围和角色显示不匹配的问题
  • 不需要给子节点Player单独加碰撞体,否则会出现碰撞冲突导致控制器移动异常

内容的提问来源于stack exchange,提问作者Sam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 01:15:11