我怎样才能用Ursina进行武器射击?

2024-04-24 06:17:31 发布

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

我目前正在使用Ursina游戏引擎,并试图制作一个基本的FPS游戏。在游戏中,显然有一把枪和一颗子弹

我的射击系统坏了。首先,它没有按照我想要的方式前进,其次,它无法充分检测碰撞

我想知道如何让枪正确地向前射出一颗子弹,以及如何让它在击中某物时告诉我

以下是我正在使用的代码:

def shoot():
    print(globalvar.currentmag, globalvar.total_ammo)
    if globalvar.currentmag > 0:
        bullet = Entity(parent=weapon, model='cube', scale=0.1, color=color.brown, collision = True, collider = 'box')
        gunshot_sfx = Audio('Realistic Gunshot Sound Effect.mp3')
        gunshot_sfx.play()
        bullet.world_parent = scene
        bullet.y = 2
        for i in range(3000):
            bullet.position=bullet.position+bullet.right*0.01
        globalvar.currentmag -= 1
        hit_info = bullet.intersects()
        print(hit_info)

        if hit_info.hit:
            print("bullet hit")
            destroy(bullet, delay=0)
        destroy(bullet, delay=1)

    else:
        reload()

我曾尝试在Ursina中使用光线投射方法,但没有成功


Tags: info游戏ifpositioncolorparentprinthit
1条回答
网友
1楼 · 发布于 2024-04-24 06:17:31

不要在创建项目符号后立即更新其位置。相反,使用它的animate_position()方法让Ursina更新它。然后可以在全局update()函数中检查冲突。确保所有涉及的实体都有一个碰撞器,并且它们可以在全球范围内访问

bullet = None
def input(key):
    global bullet
    if key == 'left mouse down':
        bullet = Entity(parent=weapon, model='cube', scale=.1, color=color.black, collider='box')
        bullet.world_parent = scene
        bullet.animate_position(bullet.position+(bullet.forward*500), curve=curve.linear, duration=2)
        destroy(bullet, delay=2)

def update():
    if bullet and bullet.intersects(wall).hit:
        hit_info = bullet.intersects(wall)
        print('hit wall')
        destroy(bullet)

我根据FirstPersonController类中的演示代码改编了这个示例。我不确定在调用bullet.intersects()时是否有不指定其他实体的方法。我认为它应该可以工作,因为hit_info对象有各种属性,其中entities但是我自己无法让它工作

相关问题 更多 >