I am asking this because I didn't find any example how to make an enemy firing a bullet at the player in Godot (just the other way round). I am also pretty new to Godot.
This is what I've tried so far:
(Bot1 (KinematicBody Enemy node):
var fire_delay
var max_fire_delay = .25
var bot1_laser_scene = preload("res://Enemies/Bot1_Laser.tscn")
func _ready():
fire_delay = max_fire_delay
add_to_group("Enemy")
func _process(delta):
var playerPosition = get_node("../Player").get_translation()
look_at(playerPosition, UP)
var follow_vect = (playerPosition-get_translation()).normalized()*delta*speed
move_and_slide(follow_vect)
shoot(delta)
func shoot(delta):
if fire_delay < 0:
var shot = bot1_laser_scene.instance()
shot.global_transform.origin = global_transform.origin
shot.rotation = rotation
get_tree().root.add_child(shot)
fire_delay = max_fire_delay
else:
fire_delay -= 1.0 * delta
(Bot1Laser (KinematicBody Enemy's Bullet (Bot1Laser) node):
extends KinematicBody
var ttl = 1.5
var speed = 2
func _ready():
pass
func _process(delta):
if ttl < 0:
queue_free()
else:
ttl -= delta
var follow_vect = get_translation().normalized()*delta*speed
translate(follow_vect)
The bots are facing into the players direction, and the shoot function is triggered regularly,
however there are not bullets fired. Any idea whats wrong here?