游戏 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,-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,-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。这有效,我试过了。

如果您还有其他问题,请随时提出。我非常乐意提供帮助。如果这样做有效,请务必upvoteapprove

,

将摄像机置于播放器的父级并将其移回。这样它就会随着玩家实体旋转。

camera.parent = player
camera.z = -10