Ursina的FirstPersonController出现问题

2024-09-27 21:28:39 发布

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

我正在使用Ursina引擎创建一个3D游戏。然而,当我尝试加载FirstPersonCharacter时,我得到的只是一个灰色背景(正常)和一个非常小的洋红色正方形,在中间,倾斜45°。那是什么

我第一次尝试为第一人称角色制作我自己的机械装置,根据鼠标位置移动相机(我有这个),我在玩数学和运动方面的东西。。。我在看这个视频(https://www.youtube.com/watch?v=DHSRaVeQxIk)的时候完全是在看别的东西,我发现了FirstPersonController

但是,使用和他(几乎)相同的代码,它不起作用!那是什么问题,有人已经碰到了吗?第一个人控制器坏了吗?还是我的脑子坏了

编辑:在ursina备忘单中发现小品红色倾斜正方形是光标。但我还是不能动,不能有重力或其他东西?我看不见我的地板

第二次编辑:使用提供、整理的ursina备忘单上的一些代码,我现在可以看到我的楼层了。但是我只能在一个轴上移动相机(上下),我不能移动,没有重力,什么也没有

这是我的密码:

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController

app = Ursina()

window.title = 'The lab'
window.borderless = False
window.fullscreen = True
window.exit_button.visible = False
window.fps_counter.enabled = True

floorcubes = []
for i in range(-20, 20, 2):
    for j in range(-20, 20, 2):
        floorcubes.append(Entity(model='cube', color=color.white, scale=(2,2,2), position = (i, 0, j)))
player = FirstPersonController()
app.run()

以下是ursina备忘单中提供的代码,稍作安排:

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
app = Ursina()
ground = Entity(model='plane', scale=(100,1,100), color=color.yellow.tint(-.2), texture='white_cube', texture_scale=(100,100), collider='box', position = (0, -2, 0), grounded = True)
e = Entity(model='cube', scale=(1,5,10), x=2, y=.01, rotation_y=45, collider='box', texture='white_cube')
e.texture_scale = (e.scale_z, e.scale_y)
e = Entity(model='cube', scale=(1,5,10), x=-2, y=.01, collider='box', texture='white_cube')
e.texture_scale = (e.scale_z, e.scale_y)

player = FirstPersonController(model='cube', y=2, origin_y=-.5, gravity = 1)
player.gun = None

gun = Button(parent=scene, model='cube', color=color.blue, origin_y=-.5, position=(3,0,3), collider='box')
gun.on_click = Sequence(Func(setattr, gun, 'parent', camera), Func(setattr, player, 'gun', gun))

gun_2 = duplicate(gun, z=7, x=8)
slope = Entity(model='cube', collider='box', position=(0,0,8), scale=6, rotation=(45,0,0), texture='brick', texture_scale=(8,8))
slope = Entity(model='cube', collider='box', position=(5,0,10), scale=6, rotation=(80,0,0), texture='brick', texture_scale=(8,8))

def input(key):
    if key == 'left mouse down' and player.gun:
        gun.blink(color.orange)
        bullet = Entity(parent=gun, model='cube', scale=.1, color=color.black)
        bullet.world_parent = scene
        bullet.animate_position(bullet.position+(bullet.forward*50), curve=curve.linear, duration=1)
        destroy(bullet, delay=1)
app.run()

多亏了答案,我现在:

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
import pyautogui
import random
import math

app = Ursina()

is_fullscreen = True
window.title = 'The lab'
window.borderless = False
window.fullscreen = is_fullscreen
window.exit_button.visible = False
window.fps_counter.enabled = True

latest_mouse_pos = pyautogui.position()
pyautogui.FAILSAFE = False
sensibility = 2.5
mouse.visible = True


floorcube = Entity(model="cube", color = color.white, scale=(20, 1, 20), collider="box", position=(0, -100, 0))


def update():
    global latest_mouse_pos
    if held_keys['f']:
        camera.fov += 1
    if held_keys['r']:
        camera.fov -= 1

player = FirstPersonController()

app.run()

Tags: fromimportboxmodelpositionwindowcolorentity
1条回答
网友
1楼 · 发布于 2024-09-27 21:28:39

有重力,它的作用是让玩家跌入无限。当你移动鼠标向上看时,你会看到立方体消失在远处

解决方法是将collider='box'添加到地板立方体中,以防止玩家摔倒。请注意,起点似乎位于其中一个立方体内,因此您必须跳出(使用空格键)或稍微降低地板立方体的位置

相关问题 更多 >

    热门问题