【发布时间】: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 如何触发下一帧进行渲染,这样您就可以每帧更新一次位置,而不是每帧更新数千次。