0

I am trying to get a bullet to originate from the tip of my gun barrel. I cannot figure it out. I've been net searching and trial and erroring for 12 hours and can't get it. The bullet needs to move with the player and originate from the gun tip. As is, the bullet originates at the crosshair. I've tried setting origin, which makes it either too far ahead or behind the player. Plus, origin needs to update with the player as well.

import random
import time
from direct.actor.Actor import Actor
from ursina import Audio, camera, Entity, invoke, mouse, raycast, scene, Ursina, Vec3
from ursina.prefabs.first_person_controller import FirstPersonController


class Bullet45(Entity):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.heading = Vec3(0, 0, 0)
        self.lifetime = 10
        self.speed = 10
        for key, value in kwargs.items():
            setattr(self, key, value)

    def update(self):
        print(self.position)
        trace = raycast(self.position, Vec3(0, 0, 0))
        # rt = raycast(self.rotation, Vec3(90, -10, 0))
        # self.position += self.heading * self.speed * time.dt
        # self.rotation += Vec3(0, 0.1, 0)


class Ingram(Entity):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.controller = None
        self.cooldown = False
        for key, value in kwargs.items():
            setattr(self, key, value)
        self.actor = Actor('./models/ingram/scene.gltf')
        self.actor.reparent_to(self)
        self.actor.setPlayRate(8.0, 'Cycle')
        self.actor.pose('Cycle', 0)
        rate = self.actor.getPlayRate('Cycle')
        self.dur = rate / (rate * 2.5)
        self.flash = Entity(parent=self,
                            model='./models/flash/scene.gltf',
                            enabled=False,
                            rotation=(0, 90, 0),
                            scale=0.005,
                            x=0.35)
        self.sound = Audio('./sounds/ingram/50_close.mp3',
                           autoplay=False,
                           volume=3)

    def input(self, key):
        if key == 'left mouse down':
            if not self.cooldown:
                self.cooldown = True
                self.shoot()

    def shoot(self):
        self.actor.play('Cycle')
        self.sound.play()
        self.flash.animate('rotation_z', random.randint(0, 360))
        self.flash.enable()
        x, y = self.parent.world_x, self.parent.world_y
        bullet = Bullet45(model='sphere',
                          heading=self.parent.forward,
                          position=self.parent.world_position + self.parent.forward,
                          rotation=self.parent.world_rotation,
                          scale=0.007,
                          speed=5)
        # bullet.animate_position(bullet.position + bullet.heading * 5, 1)
        invoke(self.flash.disable, delay=0.1)
        invoke(self.actor.pose, 'Cycle', 0, delay=self.dur)
        invoke(setattr, self, 'cooldown', False, delay=self.dur)


class Player(Entity):
    def __init__(self, **kwargs):
        self.controller = FirstPersonController(**kwargs)
        self.controller.mouse_sensitivity = (60, 60)
        super().__init__(parent=self.controller.camera_pivot)
        self.ingram = Ingram(parent=self,
                             controller=self.controller,
                             cooldown=False,
                             position=(0.4, -0.20, 0.8),
                             rotation=(90, -90, 0),
                             scale=2)

    # def update(self):
    #     print(self.world_position)


game = Ursina()
ground = Entity(model='plane', collider='mesh', scale=64, texture='grass', texture_scale=(4, 4))
player = Player()
# mouse.enabled = True
# mouse.locked = False

game.run()

I'm thinking I need some vector math, but I don't know what.

malonn
  • 59
  • 1
  • 7

0 Answers0