+1 vote

HI, ok basically i'm trying to make a gun in godot and it's not working out too well the bullets are flying everywhere not following the mouse at all except when the player isn't moving even then they're too slow and sometimes it goes the complete opposite way
i used an instance of a scene for the bullet here's my code:

func createBulletInstance() -> void:
    if timeBetweenShots <= 0 && not global.first_bullet:
        var b = bullet.instance()
        add_child(b)
        b.start(global.bullet_spawn_pos, global.dif_bullet_mouse)
        timeBetweenShots = maxTimeBetweenShots

    else:
        timeBetweenShots -= get_process_delta_time()

that's in the main scene script and here's the bullet script:

    extends Area2D

export (int) var speed
export (int) var damage
var velocity = Vector2()
var difBulletMouse: Vector2

func start(_positon, _direction):
    position = _positon
    rotation_degrees = global.gun_rotation
    velocity = _direction * speed
    print(_direction, speed)

func _process(delta: float) -> void:
#   print(velocity, delta)
    global.dif_bullet_mouse = calculateGlobalDifBulletMouse()
    position += velocity * delta

func calculateGlobalDifBulletMouse() -> Vector2:
    difBulletMouse = get_global_mouse_position() - global_position
    return difBulletMouse

here's an example of what the console prints ( _direction, speed):

i tried shooting at exactly the same place over and over

(1064.097046, -28.269012)2
(504.196075, 121.085732)2
(-1106.026978, -303.778748)2
(2494.589355, 624.708252)2
(313.460938, 33.704681)2
(-834.855591, -42.236572)2

here's the print for (velocity, delta):

(0, 0)0.016667
(0, 0)0.016667
(0, 0)0.016667

this is where i shoot

(1548.19397, 239.461975)0.016667
(0, 0)0.016667
(1548.19397, 239.461975)0.016667
(0, 0)0.016667
(1548.19397, 239.461975)0.016667
(0, 0)0.016667
(1548.19397, 239.461975)0.016667
(0, 0)0.016667

loops forever

Thanks for trying to help!!!!!!!

in Engine by (120 points)

why are you doing global.dif_bullet_mouse = calculateGlobalDifBulletMouse() in _process ?

Remove global.dif_bullet_mouse = calculateGlobalDifBulletMouse() in _process from bullet scene

i put it in the start function it works better but is still a bit weird
some of the bullets are slower and the first one didn't even fire
also it's not very precise for example if i move my mouse and click
it will not shoot where my mouse is but a where my mouse was a couple seconds ago
Thanks for your help though :)

1 Answer

+1 vote

I FIXED IT! thanks to a couple youtube videos and supper_raptor
here's the new and improved code:

func createBulletInstance() -> void:
    if timeBetweenShots <= 0:
        var b = bullet.instance()
        add_child(b)
        var dir = Vector2(1,0).rotated($gun/gun.global_rotation)
        b.start(global.bullet_spawn_pos, dir)
        timeBetweenShots = maxTimeBetweenShots

and in the bullet script:

export (int) var speed
export (int) var damage
var velocity = Vector2()
var difBulletMouse: Vector2

func start(_positon, _direction):
    position = _positon
    rotation_degrees = global.gun_rotation
    velocity = _direction * speed
    global.dif_bullet_mouse = calculateGlobalDifBulletMouse()

func _process(delta: float) -> void:
    position += velocity * delta

func calculateGlobalDifBulletMouse() -> Vector2:
    difBulletMouse = get_global_mouse_position() - global_position
    return difBulletMouse
by (120 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.