+1 vote

I would really like to know how I can make my enemy (kinematicbody2d) move around randomly and also display the movement animations when it does so? I would also like to add an area2d to it so it detects the player and chases it. Here is scene tree and also the script for enemy which I made by following a YouTube video (it worked in the video):

^ Skeleton(AnimatedSprite)
^ CollisonShape2D
^ icon(AnimatedSprite)
^ Camera2D
^ CollisionShape2D

extends KinematicBody2D

var speed = 100
var velocity = Vector2()
var state = 0
var rng = 0

func _process(delta):

    state = rand_range(0, 4)

if state == 0:
elif state == 1:
    velocity.x = speed
elif state == 2:
    velocity.x = -speed
elif state == 3:
    velocity.y = speed
elif state == 4:
    velocity.y = -speed
in Engine by (298 points)

how complicated do you want the movement to be? is there obstacles that the enemy needs to avoid?

Piggybacking on @Millard's comment, what exactly do you mean by "random?" Do you want true randomness? Is it like a Wander AI? As Millard asked, are there obstacles that the character needs to steer away from? There's a lot of details missing from the question that we need in order to best provide an answer.

1 Answer

0 votes

For 2D platformers: I like Gonkee's tutorial, he shows how to have an AI enemy follow the player, and have it move around randomly if it can't see the player.

For Top-down shooters: I found this tutorial series from jimbiv on youtube for a top-down shooter, he includes how to make an enemy AI and a patrol state as well, hope this helps you.

by (96 points)
edited by

My game is top down so it doesn't work as well :/

Let me cook something up for you...

Alright I watched through most of part 8 and 9 of his videos and they should have what you are looking for as well as the ability to do more with the concepts that he is teaching. I linked the whole 22 video playlist for you in my answer up top.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.