I need an enemy that when my character touches kills him.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By RexinBeast

I need a enemy for my character, could anybody help me create my enemy. I am new to Godot, I just picked up the engine a few days ago. When my enemy is touched, I would like my player to disappear, and when my enemy’s head is jumped on, I would like my enemy to disappear. Here is the code I used for my player:

extends KinematicBody2D

var score : int = 0

var speed : int = 200
var jumpforce : int = 490
var gravity : int = 800

var vel : Vector2 = Vector2()

onready var sprite : Sprite = get_node(“Sprite”)

func _physics_process(delta):

vel.x = 0

if Input.is_action_pressed("move_left"):
	vel.x -= speed
if Input.is_action_pressed("move_right"):
	vel.x += speed
vel = move_and_slide(vel, Vector2.UP)

vel.y += gravity * delta

if Input.is_action_just_pressed("jump") and is_on_floor():
	vel.y -= jumpforce

if vel.x < 0:
	sprite.flip_h = false
elif vel.x > 0:
	sprite.flip_h = false

I hope this helps! Thank you Godot Community!
Edit: I do not know how to make the code go into the box thing

:bust_in_silhouette: Reply From: Efigi

I think the concept of Signals will help you out a lot here: Using signals — Godot Engine (stable) documentation in English.

Essentially, just make an Area2D node on your enemy, and connect the body_entered signal to a function. In that function, if the player is above the enemy, just queue_free() the enemy. If not, queue_free() the player.

:bust_in_silhouette: Reply From: xCode Soul


I think GDQuest has video to create such as thing stomp the enemy to kill it

Link to Video

Best regards