游戏 Ursina 的第三人称视角

Cyb*_*osh 2 python panda3d

我正在尝试与 ursina 制作一款角色扮演游戏。我想让摄像机始终跟随角色的背部。我尝试使用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()```
Run Code Online (Sandbox Code Playgroud)

Sam*_*mer 5

您可能想要更改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
Run Code Online (Sandbox Code Playgroud)

但我听到你问相机怎么样!

我会推荐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()
Run Code Online (Sandbox Code Playgroud)

您需要创建一个地板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
Run Code Online (Sandbox Code Playgroud)

您会注意到我没有创建一个类(对其进行调整很容易),但我没有使用load_model. 这是因为即使您使用自己的模型,也不需要使用load_model. 只需将文件名(不带文件扩展名)作为string. 这个有效,我试过了。

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