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.