Skip to main content
  1. About
  2. For Teams
Asked
Modified 3 days ago
Viewed 55 times
0

I'm working on my school project about my first top down shooting game. Anyway after I did my project by following a tutorial from youtube I desire to add more enemies. But I've no idea how to do it?

I've tried it by using the same code with the first enemy I did but it's not working as I expect. The first enemy that I followed from tutorial works really fine. But The second enemy I do it by copy from the first one's only move forward not following where the player go, when the player touch it's harm nothing to player and the animations like walking, attacking or dead doesn't play as it should be. Here's my enemy code:

extends CharacterBody2D

@onready var main = get_node("/root/Main")
@onready var player = get_node("/root/Main/Player")

var explosion_scene := preload("res://scenes/explosion.tscn")
var item_scene := preload("res://scenes/item.tscn")

signal hit_player

var alive : bool
var entered : bool
var speed : int = 100
var direction : Vector2
const DROP_CHANCE : float = 0.1

func _ready():
    var screen_rect = get_viewport_rect()
    alive = true
    entered = false
    #pick a direction for the entrance
    var dist = screen_rect.get_center() - position
    #check if need to move horizontally or vertically
    if abs(dist.x) > abs(dist.y):
        #move horizontally
        direction.x = dist.x
        direction.y = 0
    else:
        #move vertically
        direction.x = 0
        direction.y = dist.y

func _physics_process(_delta):
    if alive:
        $AnimatedSprite2D.animation = "run"
        if entered:
            direction = (player.position - position)
        direction = direction.normalized()
        velocity = direction * speed
        move_and_slide()
        
        if velocity.x != 0:
            $AnimatedSprite2D.flip_h = velocity.x < 0
    else:
        pass

func die():
    alive = false
    $AnimatedSprite2D.stop()
    $AnimatedSprite2D.animation = "dead"
    $Area2D/CollisionShape2D.set_deferred("disabled", true)
    if randf() <= DROP_CHANCE:
        drop_item()
    var explosion = explosion_scene.instantiate()
    explosion.position = position
    main.add_child(explosion)
    explosion.process_mode = Node.PROCESS_MODE_ALWAYS

func drop_item():
    var item = item_scene.instantiate()
    item.position = position
    item.item_type = randi_range(0, 2)
    main.call_deferred("add_child", item)
    item.add_to_group("items")

func _on_entrance_timer_timeout():
    entered = true

func _on_area_2d_body_entered(_body):
    hit_player.emit()

Also it will sent the signal to enemy_spawner to spawn it on Marker2D This is my enemy_spawner code:

extends Node2D

@onready var main = get_node("/root/Main")

signal hit_p

var goblin_scene := preload("res://scenes/goblin.tscn")
var spawn_points := []

# Called when the node enters the scene tree for the first time.
func _ready():
    for i in get_children():
        if i is Marker2D:
            spawn_points.append(i)

func _on_timer_timeout():
    #check how many enemies have already been created
    var enemies = get_tree().get_nodes_in_group("enemies")
    if enemies.size() < get_parent().max_enemies:
        #pick random spawn point
        var spawn = spawn_points[randi() % spawn_points.size()]
        #spawn enemy
        var goblin = goblin_scene.instantiate()
        goblin.position = spawn.position
        goblin.hit_player.connect(hit)
        main.add_child(goblin)
        goblin.add_to_group("enemies")

func hit():
    hit_p.emit()

  • Thank you -
1
  • Your question is incomplete. First of all, this is not a Minimum reproducible example. Reduce your code so it's actually easier to focus on the real problem. "I've tried it by using the same code with the first enemy I did but it's not working as I expect" Where? The enemy_spawner code has an if statement, which executes only one time, and I don't see other parts where you "spawn" enemies. "Also it will sent the signal to enemy_spawner to spawn it on Marker2D" Who? The enemy send a signal to spawn? Please, clarify
    Roci49
    –  Roci49
    2025-10-11 18:25:20 +00:00
    Commented 2 days ago

1 Answer 1

0

Did you check the Area2D's collision layer and mask if they match with the first enemy's Area2D?

Maybe they weren't set yet so that the player can't be detected, which then ignores the animations you mentioned.

New contributor
Zen is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.