Godot Version
4
Question
`I’m trying to make a turret that will shoot if the player enters a certain area but it does not shoot
`
@onready var ray_cast = $aim
@onready var timer = $recharging
@export var ammo : PackedScene
var target
func _ready():
target = get_parent().find_child(“player”)
func _physics_process(_delta):
_aim()
_check_player_collision()
func _aim():
ray_cast.target_position = to_local(target.position)
func _check_player_collision():
if ray_cast.get_collider() == target and timer.is_stopped():
timer.start()
elif ray_cast.get_collider() != target and not timer.is_stopped():
timer.stop()
func _on_timer_timeout():
_shoot()
print(“s”)
func _shoot():
var bullet = ammo.instantiate()
bullet.position = position
bullet.direction = (ray_cast.target_position).normalized()
get_tree().current_scene.add_child(bullet)
`