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

如何让Ursina中的message实体正确朝向相机?

解决Ursina中消息实体朝向相机的问题

问题原因

你的message实体使用quad模型,它的默认正面朝向是**-Z轴**。调用self.look_at(camera)时,会让模型的-Z轴指向相机,导致模型背对着相机,因此无法看到。而camera.ui是2D UI相机,和3D场景相机的坐标系逻辑不同,所以无法实现3D视角下的正确朝向。

解决方案

方法1:使用Ursina内置的billboard属性(推荐)

直接在实体初始化时添加billboard=True,Ursina会自动处理实体朝向相机的逻辑,无需手动编写update函数:

from ursina import *

class message(Entity):
    def __init__(self, entity_position:Entity, y_up = 4, msg="☕"):
        super().__init__(
            model="quad",
            texture="src/message.png",
            position=Vec3(entity_position.position) + Vec3(0, y_up, 0),
            billboard=True  # 自动让实体始终朝向相机
        )

        self.entity_position = entity_position
        self.y_up = y_up

方法2:手动修正look_at的朝向逻辑

如果需要手动控制,可以通过look_at的forward参数,指定让模型的正面(+Z轴)指向相机:

from ursina import *

class message(Entity):
    def __init__(self, entity_position:Entity, y_up = 4, msg="☕"):
        super().__init__(
            model="quad",
            texture="src/message.png",
            position=Vec3(entity_position.position) + Vec3(0, y_up, 0)
        )

        self.entity_position = entity_position
        self.y_up = y_up

    def update(self):
        # 指定模型的forward方向(+Z)朝向相机,确保正面对准视角
        self.look_at(camera, forward=self.forward)

也可以先反转模型初始朝向,再使用常规look_at:

from ursina import *

class message(Entity):
    def __init__(self, entity_position:Entity, y_up = 4, msg="☕"):
        super().__init__(
            model="quad",
            texture="src/message.png",
            position=Vec3(entity_position.position) + Vec3(0, y_up, 0),
            rotation_z=180  # 先反转模型默认朝向
        )

        self.entity_position = entity_position
        self.y_up = y_up

    def update(self):
        self.look_at(camera)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 21:43:27