Topic was automatically imported from the old Question2Answer platform.
Asked By
IranosMorloy
Old Version
Published before Godot 3 was released.
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.
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 “first_weapon” is Position2D node. The bullet scene is loaded for the bullet_scene var.
When (and where) do you call the shooting() function?
Give us more details and/or attach your project!
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
IranosMorloy | 2017-11-07 12:12
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()
(...)
bruteforce | 2017-11-07 12:52
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?
IranosMorloy | 2017-11-07 13:33
OK, a quick sample (it may not be the optimal solution):
1.) Modify the shooting() function (swap these two lines):