0

The following code creates a 3d world that allows the user to navigate with two weapons. Ten soldiers spawn within the world and everytime the user kills one, they respawn in a random position. My goal is to get the soldiers to be constantly moving within the four walls, but keep getting errors in my attempt to do so. I am using pycharm and the 3d engine, Ursina in order to do this.plz help.

    import timeit
from ursina import *
from random import uniform
from ursina.prefabs.first_person_controller import FirstPersonController


class Player(Entity):
    def __init__(self, **kwargs):
        self.controller = FirstPersonController(**kwargs)
        super().__init__(parent=self.controller)

        self.gun = Entity(parent=self.controller.camera_pivot,
                          scale=.012,
                          model="finalassets/shotgun/source/C96.FBX",
                          texture="finalassets/shotgun/textures/C96_Albedo.png",
                          position=Vec3(0.1, -.5, 0.98),
                          rotation=Vec3(1, -10, 3),
                          visible=True)

        self.ar = Entity(parent=self.controller.camera_pivot,
                         scale=.05,
                         position=Vec3(0, -.6, 1),
                         rotation=Vec3(15, 85, 5),
                         model="finalassets/ace/Source/ace.FBX",
                         texture="finalassets/ace/Source/textures/ACE_gloss.png",
                         visible=False)

        self.weapons = [self.gun, self.ar]
        self.current_weapon = 0
        self.switch_weapon()

    def switch_weapon(self):
        for i, v in enumerate(self.weapons):
            if i == self.current_weapon:
                v.visible = True
            else:
                v.visible = False

    def input(self, key):
        try:
            self.current_weapon = int(key) - 1
            self.switch_weapon()
        except ValueError:
            pass

        if key == "scroll up":
            self.current_weapon = (self.current_weapon + 1) % len(self.weapons)
            self.switch_weapon()

        if key == "scroll down":
            self.current_weapon = (self.current_weapon - 1) % len(self.weapons)
            self.switch_weapon()

        if key == "left mouse down":
            if self.current_weapon == 0:
                Bullet(model='sphere', color=color.white, scale=0.05,
                       position=self.controller.camera_pivot.world_position,
                       rotation=self.controller.camera_pivot.world_rotation)
            elif self.current_weapon == 1:
                Bullet(model='sphere', color=color.red, scale=0.1,
                       position=self.controller.camera_pivot.world_position,
                       rotation=self.controller.camera_pivot.world_rotation)

    def update(self):
        self.controller.camera_pivot.y = 2 - held_keys['left control']


class Bullet(Entity):
    def __init__(self, speed=50, lifetime=10, **kwargs):
        super().__init__(**kwargs)
        self.speed = speed
        self.lifetime = lifetime
        self.start = time.time()

    def update(self):
        ray = raycast(self.world_position, self.forward, distance=self.speed * time.dt)
        if not ray.hit and time.time() - self.start < self.lifetime:
            self.world_position += self.forward * self.speed * time.dt
        else:
            destroy(self)
            if ray.hit and isinstance(ray.entity, EntityWithHealth):
                entity = ray.entity
                entity.take_damage(1)
                if entity.health <= 0:
                    destroy(entity)
                    spawn_entities()

    def handle_collision(self, hit):
        pass


class EntityWithHealth(Entity):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.health = 2

    def take_damage(self, damage):
        self.health -= damage

    def kill(self):
        destroy(self)
    def spawn(self):
        self.position = Vec3(
            uniform(-25, 25),
            0,
            uniform(-25, 25)
        )
        self.health = 2

app = Ursina()

Sky()

ground = Entity(
    model='plane',
    scale=(100, 1, 100),
    color=color.lime,
    texture='grass',
    texture_scale=(150, 150),
    collider='box'
)

wall = Entity(model="cube", collider="box", position=(0, 0, 50), scale=(100, 10, 50), rotation=(0, 0, 0),
              texture="finalassets/wall/textures/brick7.jpg", texture_scale=(35, 35), color=color.black)
wall2 = duplicate(wall, z=-50)
wall3 = duplicate(wall, rotation_y=90, x=-50, z=0)
wall4 = duplicate(wall3, x=50)

player = Player(position=(0, 1, 0))

entities = []

for _ in range(10):
    entity = EntityWithHealth(
        model="finalassets/soldier/soldier_airborne.fbx.glb",
        color=color.orange,
        position=Vec3(uniform(-25, 25), 0, uniform(-25, 25)),
        scale=0.0015,
        collider='box'
    )
    entities.append(entity)

def spawn_entities():
    for entity in entities:
        destroy(entity)

    for _ in range(10):
        entity = EntityWithHealth(
            model='finalassets/soldier/soldier_airborne.fbx.glb',
            color=color.green,
            position=Vec3(
                uniform(-25, 25),
                0,
                uniform(-25, 25)
            ),
            scale=0.0015,
            collider='box'
        )
        entities.append(entity)

spawn_entities()

def input(key):
    if key == 'space':
        spawn_entities()

def update():
    for entity in entities:
        direction = Vec3(uniform(-1, 1), 0, uniform(-1, 1)).normalized()
        speed = 3
        entity.position += direction * speed * time.dt

app.run()
  • Your code does not seem to include any movement code. It's generally expected on SO to include your own effort and ask questions based upon that effort. Since you mentioned you are having errors with the movement code you wrote, edit that into the question and rephrase the title and body to reflect the error. – youngson May 08 '23 at 09:03

0 Answers0