0 votes

Hi i'm new to game developpement and am trying to make a gun,
it follows the mouse and it can spawn bullets using instances of this code but for some reason the bullets wont move, i've been trying to fix this issue for over 3hours please help
here's my code:

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

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


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

ps: the speed is set to 200 so that shouldn't be a problem

and here's the code for the _direction:

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

Thanks for checkign this out!!

in Engine by (120 points)

2 Answers

0 votes

Hi,
I am sorry i don't know what is wrong with your code.

I tried this code and does not have any problem (the sprite is moving):

extends Area2D

var velocity = Vector2()

func start():
    position = Vector2(10, 10)
    velocity = Vector2(1, 1) * 10

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

The objects used for my test:

Area2D
- CollisionPolygon2D
    - Sprite

I don't know what is wrong in your game:
Maybe you forgot to set the speed to a value greater than 0, or maybe your object is colliding with another and that keeps him from moving.

I'm not very experienced with Godot so I don't know what to say more.

by (50 points)
edited by

i tried adding a collision shape to my bullet but that didn't work
still thanks for your help

0 votes

Ok 1 day later I found the problem it was having the speed set to 0 on the bullet scene

by (120 points)

Sorry I hadn't seen your message before. I said in my message that you may have forgotten to set a speed greater than zero . You haven't read my answer entirely .
That's good if you did solve your problem.

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.