The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Hey all,

I have a problem. I watched some tutorial videos to know how to create the code for shooting. But even if I tried some different codes, my character still can't shoot a bullet. Can anyone help me?

My character can actually move and rotate in any direction. It is a 2D board game. So I want it to shoot in the current direction it is facing at the moment.

I've written down this function for shooting:

func shooting():
var bullet = bullet_scene.instance()
get_parent().add_child(bullet)
bullet.set_global_pos(get_node("first_weapon").get_global_pos())
bullet.set_linear_velocity(Vector2(sin(get_rot()) * bullet_speed, cos(get_rot()) * bullet_speed))
pass

By default the shooting is set to false. I want to set it true by pressing a key, so I also have:

    if Input.is_action_press("btn_spacebar"):
    shooting = true

In _ready() function, I have "shooting()".

The bullet is a RigidBody2D without any script, just deactivated gravity and friction. The "firstweapon" is Position2D node. The bullet scene is loaded for the bulletscene var.

So what I am doing wrong?

Thanks in advance.

in Engine by (64 points)
retagged by

1 Answer

+3 votes
Best answer
shooting = true

This is a simple boolean variable.

When (and where) do you call the shooting() function?

Give us more details and/or attach your project!

by (685 points)
selected by

This is all I've got so far at this moment. Reminder: I am a true beginner at programming... :) Nothing more, nothing less. This script is attached to the player.

    extends KinematicBody2D

#Základní pohyb
export var player_speed = 150
export var player_acceleration = 30
export var rotation_speed = 10

#Životnost
export (int) var max_health = 500
export (int) var current_health = 500

#Zbraně
#export (PackedScene) var bullet_scene
#export (NodePath) var bullet_path_a
#export (NodePath) var bullet_path_b
onready var bullet_scene = preload("res://Scenes/bullet.xml")
var bullet_speed = 400

var shooting = false
var killed = false

var velocity = Vector2()

func _ready():
    set_fixed_process(true)
    set_process_input(true)
    set_process(true)

func _fixed_process(delta):
    if Input.is_action_pressed("btn_A"):
        set_rot(get_rot() + delta * rotation_speed)
    if Input.is_action_pressed("btn_D"):
        set_rot(get_rot() + delta * -rotation_speed)
    if Input.is_action_pressed("btn_W"):
        move(Vector2((player_speed * sin(get_rot())) * delta, (player_speed * cos(get_rot())) * delta))
    if Input.is_action_press("btn_spacebar"):
        shooting = true

    var motion = velocity * delta
    move(motion)

func shooting():
    var bullet = bullet_scene.instance()
    get_parent().add_child(bullet)
    bullet.set_global_pos(get_node("first_weapon").get_global_pos())
    bullet.set_linear_velocity(Vector2(sin(get_rot()) * bullet_speed, cos(get_rot()) * bullet_speed))
    pass

So, you have a shooting() function, but you don't use it anywhere, as I thought.

Try to call it :)

func _fixed_process(delta):
    (...)
    if Input.is_action_press("btn_spacebar"):
        shooting()
    (...)

Still not working. Or I can't see it, not sure. The bullet is a rigitbody2d and it has just 2 children at this moment, a sprite and a collisionshape2d. Or should I take it 2 parents above the movable player node?

EDIT: Yeah, it is working!!!! I just had a grammar mistake.... but it works now!!! Thank you very much! God bless you! :-)

EDIT 2: And as long as you are here, may I have another question? How to make the bullet disappear after, let's say, 1000 pixels away? I know about the queue.free() method, but how to tell the game about it?

OK, a quick sample (it may not be the optimal solution):

1.) Modify the shooting() function (swap these two lines):

func shooting():
    (...)
    bullet.set_global_pos(get_node("first_weapon").get_global_pos())
    get_parent().add_child(bullet)
    (...)

2.) Attach this script to the bullet (and analyze it):

extends RigidBody2D

const MAX_DISTANCE = 300

var startPos = null

func _ready():
    set_fixed_process(true)
    startPos = get_global_pos()

func _fixed_process(delta):
    var currentPos = get_global_pos()
    var distance = (currentPos - startPos).length()

    if(distance >= MAX_DISTANCE):
        print("I reached the MAX_DISTANCE")
        queue_free()

This works right as well! Thank you again! :-)

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 Frequently asked questions and 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 [email protected] with your username.