游戏《乌尔西娜》的第三人称POV

2024-04-24 17:18:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想和乌西娜做一个rpg风格的游戏。我想让相机始终跟随角色的后面。我试着使用相机。看着(播放器),但当相机旋转时,我无法让它旋转到角色的后面

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()```

Tags: app角色modelifinitdefkeysrpg
2条回答

您可能需要更改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中即可。这很有效,我试过了

如果你还有什么问题,尽管问吧。我非常乐意帮忙。如果这有效,请确保upvoteapprove

将摄影机设置为播放机的父对象,然后将其向后移动。这样,它将与玩家实体一起旋转

camera.parent = player
camera.z = -10

相关问题 更多 >