【问题标题】:How can I make a weapon shoot using Ursina?如何使用 Ursina 进行武器射击?
【发布时间】:2021-09-08 10:17:32
【问题描述】:

我目前正在使用 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 中使用 raycast 方法,但没有成功。

【问题讨论】:

  • 请提供预期的minimal, reproducible example (MRE)。我们应该能够复制和粘贴您的代码的连续块,执行该文件,并重现您的问题以及跟踪问题点的输出。这让我们可以根据您的测试数据和所需的输出来测试我们的建议。显示中间结果与您的预期不同的地方。 “没有工作”不是问题规范。
  • 我不知道 Ursina 是如何工作的,但看起来在运行 for 循环之后,项目符号会在第一帧渲染之前离屏幕很远。
  • 您需要弄清楚 Ursula 如何触发下一帧进行渲染,这样您就可以每帧​​更新一次位置,而不是每帧更新数千次。

标签: python ursina


【解决方案1】:

不要在创建项目符号后立即更新它的位置。相反,使用它的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,但我自己无法让它工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-03
    • 2018-11-17
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多