0 votes

i am trying to program a 2.5d enemy and its giving some problems.

i have put an area2d around it to detect when player is nearby and chase him and attack him and when player is not in the area2d u go back to normal idle state. but my enemy when i leave the area2d keeps walking on same spot and doesnt go to idle state.

here is my code:

`extends KinematicBody2D

export var acceleration = 300
export var max_speed = 50
export var friction = 200

enum {
idle,
wander,
walk
}

var velocity = Vector2.ZERO
var state = walk

onready var playerdetectionzone = $playerdetectionzone
onready var animationplayer = $AnimationPlayer
onready var animationtree = $AnimationTree
onready var animationstate = animationtree.get("parameters/playback")

func _ready():
animationtree.active = true

func physicsprocess(delta):
match state:
idle:
velocity = velocity.movetoward(Vector2.ZERO, friction * delta)
seek
player()
walk:
var player = playerdetectionzone.player
animationstate.travel("walk")
if player != null:
acceleratetowardspoint(player.globalposition,delta)
else:
state = idle
velocity = move
and_slide(velocity)

func acceleratetowardspoint(point,delta):
var direction = globalposition.directionto(point)
velocity = velocity.movetoward(direction * maxspeed, acceleration * delta)
animationtree.set("parameters/walk/blend_position",direction)

func seekplayer():
if playerdetectionzone.can
see_player():
state = walk`

in Engine by (28 points)

Please log in or register to answer this question.

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 Frequently asked questions and 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 [email protected] with your username.