Godot Version
4.3
Question
My enemy “Lawman” shoots bullets at the player. My shoot script is below. I made a horse enemy that the lawman rides on. However, when I run it, all the code works as normal, the bullets instantiate, but but they do not appear. Not sure how to fix this.
# lawman script
extends Area2D
# detection
var player_agro = false
var player = null
#animations
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
# aiming
@onready var ray_cast_2d: RayCast2D = $RayCast2D
@onready var timer: Timer = $Timer
@export var ammo : PackedScene
# Health
var enemyHealth = 3
func _ready(): IP.set_meta(&'lawman', self)
func _physics_process(_delta: float) -> void:
aim()
_check_player_collision()
func aim():
if player_agro == true:
ray_cast_2d.target_position = ray_cast_2d.to_local(IP.get_meta(&'player').global_position)
# this is what spawns each bullet
func _shoot():
if player_agro == true:
var bullet = ammo.instantiate()
bullet.position = position
bullet.direction = (ray_cast_2d.target_position).normalized()
get_tree().current_scene.add_child(bullet)
print(bullet)
func _on_timer_timeout():
_shoot()
func _check_player_collision():
if ray_cast_2d.get_collider() == player and timer.is_stopped():
timer.start()
elif ray_cast_2d.get_collider() != player and not timer.is_stopped():
timer.stop()
# detect player
func _process(_delta: float) -> void:
if player_agro == true:
if player.position.x < position.x:
animated_sprite.flip_h = true
else:
animated_sprite.flip_h = false
func _on_detection_area_body_entered(body: Node2D) -> void:
player = body
player_agro = true
animated_sprite.play("agro_anim")
func _on_detection_area_body_exited(_body: Node2D) -> void:
player = null
player_agro = false
animated_sprite.play("idle")
func decrease_health():
print(enemyHealth)
enemyHealth = enemyHealth - 1
if enemyHealth <= 0:
queue_free()
# horse script
extends Area2D
const speed = 60
var direction = 1
@onready var ray_cast_right: RayCast2D = $RayCastRight
@onready var ray_cast_left: RayCast2D = $RayCastLeft
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
var hornesHP = 3
func _process(delta):
if ray_cast_right.is_colliding():
if not ray_cast_right.get_collider().name == "player":
direction = -1
if ray_cast_left.is_colliding():
if not ray_cast_left.get_collider().name == "player":
direction = 1
if direction > 0:
animated_sprite.flip_h = true
if direction < 0:
animated_sprite.flip_h = false
position.x += direction * speed * delta
animated_sprite.play("walk")
func decrease_honse_hp():
direction *= -1
hornesHP -= 1
if hornesHP <= 0:
queue_free()