I don't know how to create a Slime (Enemy)

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

Hey,
I’m creating my first game, its a 2D zelda-like.
There’s a bunch of things on my list that I don’t know how to create and sometimes its kinda hard to follow tutorials online, so I’m hoping I can get some help here from the big boys of godot.
I have to create 2 Enemies, a Slime and a Slime Boss, I want the slime to follow the player when he’s in range but I don’t know how the slime would attack (do damage to do player) and I don’t know how to do it with GDScript, same applies to the Slime Boss, I haven’t created a health system or damage system but I also know I’m going to have problems with that.
I don’t know how to show the progress of this project so I’ll try to cover some things up

My player:
KinematicBody2D
AnimatedSprite
CollisionPolygon2D

extends KinematicBody2D

export (int) var speed

var touch_left : = false
var touch_right : = false
var touch_up : = false
var touch_down : = false
var velocity : = Vector2()
var is_walking := false

func get_input():
	if (not is_walking) and (Input.is_action_pressed("ui_right") or touch_right):
		velocity.x += 1
		$AnimatedSprite.flip_h = false
		is_walking = true
		$AnimatedSprite.play("Run")
	elif (not is_walking) and (Input.is_action_pressed("ui_left") or touch_left):
		velocity.x -= 1
		$AnimatedSprite.flip_h = true
		is_walking = true
		$AnimatedSprite.play("Run")
	elif (not is_walking) and (Input.is_action_pressed("ui_down") or touch_down):
		velocity.y += 1
		is_walking = true
		$AnimatedSprite.play("Walk_Down")
	elif (not is_walking) and (Input.is_action_pressed("ui_up") or touch_up):
		velocity.y -= 1
		is_walking = true
		$AnimatedSprite.play("Walk_Up")
		
	if Input.is_action_just_released("ui_right"):
		$AnimatedSprite.play("Idle")
		is_walking = false
		velocity.x = 0
		
	if Input.is_action_just_released("ui_left"):
		$AnimatedSprite.flip_h = true
		$AnimatedSprite.play("Idle")
		is_walking = false
		velocity.x = 0
		
	if Input.is_action_just_released("ui_up"):
		$AnimatedSprite.play("Idle_Back")
		is_walking = false
		velocity.y = 0
		
	if Input.is_action_just_released("ui_down"):
		$AnimatedSprite.play("Idle_Front")
		is_walking = false
		velocity.y = 0
	
	velocity = velocity.normalized() * speed
	
func _on_left_pressed():
	touch_left = true

func _on_right_pressed():
	touch_right = true

func _on_down_pressed():
	touch_down = true

func _on_up_pressed():
	touch_up = true

func _on_left_released():
	touch_left = false
	$AnimatedSprite.flip_h = true
	$AnimatedSprite.play("Idle")
	velocity.x = 0
	is_walking = false

func _on_right_released():
	touch_right = false
	$AnimatedSprite.play("Idle")
	velocity.x = 0
	is_walking = false

func _on_down_released():
	touch_down = false
	$AnimatedSprite.play("Idle_Front")
	velocity.y = 0
	is_walking = false

func _on_up_released():
	touch_up = false
	$AnimatedSprite.play("Idle_Back")
	velocity.y = 0
	is_walking = false

# warning-ignore:unused_argument
func _physics_process(delta):
	get_input()
#	velocity = move_and_slide(velocity)
	move_and_collide(velocity * delta)

So first thing, about sharing your project, you can add it to a git repositor, like GitHub, GitLab or BitBucket, and make it public, don’t forget to add a license file. So anyone can just go and look at it, but if that is not what you want, snippets of code should do fine. An extra bonus is that your project stays saved in more than one place besides your computer.

Now for your slime to detect your player. You add a RayCast2D or an Area2D to your slime and check if there is any collision during the physics step, physics_process method, if there is and the object you are colliding with is the player, you start the attack animation and cause the damage in due time.

Of course this is a very superficial explanation, but it should at least give you an idea of how to proceed and what to search for and ask more specific questions of what help you need.

tastyshrimp | 2019-12-24 12:40