#directional shooting code help

6 messages · Page 1 of 1 (latest)

paper dune
#

i wana make it so the player can oly shoot in the direction they are looking this is my curent shooting code extends Node2D

var bullet_scenes = {
0: preload("res://ice_bullet.tscn"),
1: preload("res://thunder_bullet.tscn"),
2: preload("res://flame_bullet.tscn")
}

func fire():
if not PlayerStats.has_ammo():
print("No ammo!")
return

var bullet_scene = bullet_scenes.get(PlayerStats.current_bullet)
if bullet_scene == null:
    print("Bullet scene not found!")
    return

var bullet = bullet_scene.instantiate()

var mouse_pos = get_global_mouse_position()
var direction = (mouse_pos - global_position).normalized()
var angle = direction.angle()

bullet.dir = angle
bullet.pos = global_position
bullet.rot = angle
bullet.dmg = PlayerStats.bullet_dmg

get_parent().add_child(bullet)
PlayerStats.use_ammo()

func _process(delta):
if Input.is_action_just_pressed("shoot"):
fire()

wary sentinel
#

What isn't working right now? Or rather, what is happening with this code?

paper dune
#

i dont want the player to be able to shoot backwards the implementation i had triedd only allowd the player to shoot right even if they were looking left so thats my curent shooting code after i remove the onld implementation

#

this was the old code var bullet_scenes = {
0: preload("res://ice_bullet.tscn"),
1: preload("res://thunder_bullet.tscn"),
2: preload("res://flame_bullet.tscn")
}

func can_fire_in_direction() -> bool:
var to_mouse = get_global_mouse_position() - global_position
var angle = to_mouse.angle()
var angle_deg = angle * 180.0 / PI

var facing_right = $"..".scale.x > 0

if facing_right:
    return angle_deg >= -60 and angle_deg <= 60
else:
    return angle_deg <= -120 or angle_deg >= 120

func fire():
if not PlayerStats.has_ammo():
print("No ammo!")
return

var bullet_scene = bullet_scenes.get(PlayerStats.current_bullet)
if bullet_scene == null:
    print("Bullet scene not found!")
    return

var bullet = bullet_scene.instantiate()

var mouse_pos = get_global_mouse_position()
var direction = (mouse_pos - global_position).normalized()
var angle = direction.angle()

bullet.dir = angle
bullet.pos = global_position
bullet.rot = angle
bullet.dmg = PlayerStats.bullet_dmg  # optional

get_parent().add_child(bullet)
PlayerStats.use_ammo()

func _process(delta):
if Input.is_action_just_pressed("shoot"):
if can_fire_in_direction():
fire()

wary sentinel
#

I'm not sure what this returns (it's syntax I have never used):

        return angle_deg >= -60 and angle_deg <= 60```
But if you mean to return true or false here, I would just get the player's facing angle, then see if the mouse_direction is more/less than 60 degrees from it. Return true if witin that range, otherwise false.

And if you're checking the facing and mouse direction in `can_fire_inDirection()` you no longer need the stuff checking it in `fire()`.
#

What I mostly mean is that regardless of facing right/left, the player is always facing "some direction" and you just care if the mouse is aimed within 60 degrees of it, so it doesn't need a separate check for left/right.