简体   繁体   English

如何使用 Ursina 实现敌人碰撞并在 Ursina 中更改子弹方向?

[英]How do I implement enemy collision using Ursina and change bullet direction in Ursina?

I was making an Ursina game, annd when I went to the enemy collision, it makes an error like this:我正在做一个 Ursina 游戏,当我去敌人碰撞时,它会出现这样的错误:

Traceback (most recent call last):
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\main.py", line 126, in _update
    __main__.update()
  File "C:/Users/Minerva Panganiban/OneDrive/Desktop/Python/Ursinale.py", line 128, in update
    destroy(bullet, enemy)
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\ursinastuff.py", line 48, in destroy
    s = Sequence(
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\sequence.py", line 42, in __init__
    self.generate()
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\sequence.py", line 52, in generate
    self.duration += arg.duration
TypeError: unsupported operand type(s) for +=: 'int' and 'Entity'
Traceback (most recent call last):
  File "C:/Users/Minerva Panganiban/OneDrive/Desktop/Python/Ursinale.py", line 136, in <module>
    app.run()
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\main.py", line 234, in run
    super().run()
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\direct\showbase\ShowBase.py", line 3325, in run
    self.taskMgr.run()
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\direct\task\Task.py", line 546, in run
    self.step()
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\direct\task\Task.py", line 500, in step
    self.mgr.poll()
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\main.py", line 126, in _update
    __main__.update()
  File "C:/Users/Minerva Panganiban/OneDrive/Desktop/Python/Ursinale.py", line 128, in update
    destroy(bullet, enemy)
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\ursinastuff.py", line 48, in destroy
    s = Sequence(
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\sequence.py", line 42, in __init__
    self.generate()
  File "C:\Users\Minerva Panganiban\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\sequence.py", line 52, in generate
    self.duration += arg.duration
TypeError: unsupported operand type(s) for +=: 'int' and 'Entity'

Looking at the error, I couldn't find the issue.查看错误,我找不到问题。

Here's the code of the game I'm making:这是我正在制作的游戏代码:

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

app = Ursina()

class Ground(Entity):
    def __init__(self):
        super().__init__(
            parent = scene,
            model = 'plane',
            texture = "Cube",
            scale = 500,
            position = Vec3(0, 0, 0),
            collider = 'plane')

class Cube(Entity):
    def __init__(self):
        super().__init__(
            parent = scene,
            model = 'cube',
            texture = "Sun",
            scale = 50,
            position = Vec3(250, 0, 250),
            collider = 'box')

class Cube2(Entity):
    def __init__(self):
        super().__init__(
            parent = scene,
            model = 'cube',
            texture = "Sun",
            scale = 50,
            position = Vec3(-250, 0, -250),
            collider = 'box')

class Cube3(Entity):
    def __init__(self):
        super().__init__(
            parent = scene,
            model = 'cube',
            texture = "Sun",
            scale = 50,
            position = Vec3(-250, 0, 250),
            collider = 'box')

class Cube4(Entity):
    def __init__(self):
        super().__init__(
            parent = scene,
            model = 'cube',
            texture = "Sun",
            scale = 50,
            position = Vec3(250, 0, -250),
            collider = 'box')

class Sky(Entity):
    def __init__(self):
        super().__init__(
            parent = scene,
            model = 'sphere',
            texture = "Bla",
            scale = 2050,
            double_sided = True)

class Weapon(Entity):
    def __init__(self):
        super().__init__(
            parent = camera.ui,
            model = 'cube',
            texture = "Untitled",
            scale = 0.5,
            rotation = Vec3(0, 0, 5),
            position = Vec3(0, 0, 0))

    def active(self):
        self.rotation = Vec3(0, 45, 5)
        self.position = Vec3(0, 0, 1)

    def passive(self):
        self.rotation = Vec3(0, 0, 5)
        self.position = Vec3(0, 0, 0)

player = FirstPersonController(model='cube',
                            texture = "face",
                            scale=0.75,
                            collider = 'box',
                              speed = 2.255)
camera.z = -5
ground = Ground()
cube = Cube()
cube2 = Cube2()
cube3 = Cube3()
cube4 = Cube4()
sky = Sky()
weapon = Weapon()

window.title = 'Ursinale'
window.borderless = False
window.fullscreen = False
window.exit_button.visible = False
window.fps_counter_enabled = True
window.color = color.black

enemy = Entity(parent = scene,
                 model = 'cube',
                 texture = "ArcataPonte1",
                 position = Vec3(2000, 2000, 2000),
                 scale = 25,
                 collider = 'box')
enemy.add_script(SmoothFollow(target=player,
                                offset=[0,0,0],
                                speed = 0.255))

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():
    global enemy
    if bullet and bullet.intersects(enemy).hit:
        hit_info = bullet.intersects(enemy)
        print('enemy hit')
        destroy(bullet, enemy)
        enemy = None

    if held_keys['left mouse']:
        weapon.active()
    else:
        weapon.passive()

app.run()

So is there any error, or Ursina is bugged?那么是否有任何错误,或者 Ursina 被窃听了? Another question is how do you change the direction of the bullet to what the player is facing?另一个问题是如何将子弹的方向改变为玩家面对的方向?

destroy(bullet, enemy) is wrong. destroy(bullet, enemy)是错误的。 The second parameter is the delay and should be a number, not an Entity.第二个参数是延迟,应该是一个数字,而不是一个实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM