python - 游戏 Ursina 的第三人称视角
问题描述
我正在尝试用 ursina 制作一个 rpg 风格的游戏。我想让相机始终跟随角色的背后。我尝试使用 camera.look_at(player) 但是当它旋转时我无法让相机旋转到角色的后面。
app = Ursina()
class character(Entity):
def __init__(self):
super().__init__(
model = load_model('cube'),
color = color.red,
position = (-0, -3, -8)
)
player = character()
print(player.forward)
print(player.forward)
camera.look_at(player)
player.rotation_y =180
def update():
if held_keys['a']:
player.rotation_y -= 2
if held_keys['d']:
player.rotation_y += 2
app.run()```
解决方案
您可能想要更改origin
. 也使用parents
. 我稍后会解释这意味着什么。
将origin
(移动和旋转实体的点)更改为它后面的点。
例如
from ursina import * # import urisna
app = Ursina() # make app
player = Entity(model='cube', # this creates an entity of a cube
origin = (0, 0, -2)) #now you have a origin behind the entity
app.run() #run app
但是相机呢,我听到你问了!
我会推荐ursina.prefabs.first_person_controller
它可能是为第一人控制而设计的,但您可以将其用于您的目的。
# start by doing the normal thing
from ursina import *
# but also import the first person prefab
from ursina.prefabs.first_person_controller import FirstPersonController
app = Ursina()
# create camera and player graphic
cam = FirstPersonController()
player = Entity(model='cube',
origin = (0, 0, -2),
parent = cam)
# run
app.run()
您将需要创建一个楼层entity
。
这就是第三人称控制器所需要的一切。父母和出身确保了这一点。它内置了WASD和箭头键控制,也有鼠标控制。
@Cyber-Yosh最近为这篇文章提出了一个问题,关于如何在没有第一人称控制器的情况下使用它。就是这样。我已经评论了这些变化。
from ursina import * # import as usual
app = Ursina() # create app as usual
window.fps_counter.enabled = False # this is just to remove the fps counter
box = Entity(model='cube', # create cube as before (you can make your own class)
origin=(0,0.7,-5), # set origin to behind the player and above a little
parent=camera, # make it orientate around the camera
color=color.red, # change the color
texture='shore') # choose a nice texture
def update(): # define the auto-called update function
if held_keys['a']:
camera.rotation_y -= 10 * time.dt # the time.dt thing makes it adapt to the fps so its smooth
elif held_keys['d']:
camera.rotation_y += 10 * time.dt
Sky() # just a textured sky to make sure you can see that you are both rotating
app.run() # run
你会注意到我没有创建一个类(适应这个很容易),但我没有使用load_model
. 这是因为即使您使用自己的模型,也不需要使用load_model
. 只需将文件名(不带文件扩展名)作为string
. 这行得通,我试过了。
如果您还有其他问题,请随时提问。我很乐意提供帮助。如果这有效,请确保upvote
和approve
。
推荐阅读
- shell - Gradle:无法使用 shell 命令初始化变量
- c# - Linq to SQL 属性映射到表
- plotly - 覆盖默认事件处理程序行为的正确方法是什么(例如,单击时切换跟踪可见性)?
- python - 字符串连接/索引给出 IndexError
- spring - 具有 OAuth2-ResourceServer 的 Spring 微服务仅返回 String 作为主体而不是我的自定义用户
- java - Apache POI 不更新值
- html - 样式降价的最佳方式
- webpack - 在 Webpack 4 中禁用树抖动
- php - 连接多个角度应用程序的最佳实践?
- wpf - ListBox 内部 ListBox 虚拟化