How to set sprite directions for a follow ai

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

I have a follow script on my enemy which works but i cant seem to get the sprite directions correct when following the player

the current script for the sprite direction i use is

if player_direction_x>0 and player_direction_x>player_direction_y: spritedirection=right

if player_direction_y>0 and player_direction_y>player_direction_x spritedirection=down

if player_direction_x<0 and player_direction_x<player_direction_y: spritedirection=left

if player_direction_y<0 and player_direction_y<player_direction_x spritedirection=up

as you can see its not very good

If any of you guys have any knowledge on how to do this please send an example
Thank you.

:bust_in_silhouette: Reply From: kidscancode

Your code doesn’t make much sense out of context. What type of nodes are you using for the player and enemy? 2D nodes have a built-in direction property, so you don’t need to make your own variables for this.

See the following tutorial in the docs for examples of 2D movement. There’s a “follow mouse” example which you could use for this:

i already have a working follow player ai the problem is the sprite animation for it.

like is the player is above self, play walkup if the player is to the right of self, play walkright

Newby | 2019-02-15 22:09

I see. Again, without seeing your movement code, I can’t really tell you. For example:

  • If the object is moving, how are you telling it what direction to move? You can probably set the animation that matches at the same time.
  • Are you allowing diagonal movement or only up/down/left/right? This would make things slightly more complicated.

Post your code so we can see what you have. Then it will be possible to answer your question.

kidscancode | 2019-02-16 17:57

Hello again busy with school stuff any way here’s the code that is for the movement and sprite animation

it extends kinematicbody2d

func follow_loop()://FOLLOW AI
    	if turntime>0:
    		turntime-=1
    	var target = get_parent().get_node("Player")
    	var direction = target.get_global_position() - self.get_global_position()
    	var to_player = target.global_position - self.global_position
    	distance = to_player.length()
    	if distance < blindspot and distance > atkrange:
    		var X=int(direction.x)
    		var Y=int(direction.y)
    		if turntime==0:
    			if Y>0 and Y>X:
    				turntime=10
    				movedir=dir.Down
    			if X<0 and X<Y:
    				turntime=10
    				movedir=dir.Left
    			if Y<0 and Y<X:
    				turntime=10
    				movedir=dir.Up
    			if X>0 and X>Y:
    				turntime=10
    				movedir=dir.Right
    		if X==Y and X>0 and Y>0:
    			movedir=Vector2(1,1)
    		if X==Y and X<0 and Y<0:
    			movedir=Vector2(-1,-1)
    		if X>0 and Y<0:
    			movedir=Vector2(1,-1)
    		if X<0 and Y>0:
    			movedir=Vector2(-1,1)
    		motion=direction.normalized()*SPEED
    		movement_loop()
    	else:
    		movedir=Vector2(0,0)
    
    func movement_loop():// MOVEMENT
    	if CLASS=="BOSS":
    		motion=movedir.normalized()*SPEED
    	else:
    		if hitstun==0:
    			motion=movedir.normalized()*SPEED
    		else:
    			motion=knockdir.normalized()*knockspeed
    	move_and_slide(motion, Vector2(0,0))
    
    func spritedir_loop()://CHECKS DIRECTION WHERE KINEMATIC BODY IS MOVING
    	match movedir:
    		Vector2(0,-1):
    			spritedir="Up"
    		Vector2(0,1):
    			spritedir="Down"
    		Vector2(-1,0):
    			spritedir="Left"
    		Vector2(1,0):
    			spritedir="Right"
    			
    func anim_switch(animation):// CHANGES ANIMATION
    	var newanim=str(animation,spritedir)
    	if $anim.current_animation!=newanim:
    		$anim.play(newanim)

It is my script i use for all my kinematic bodies

i call them with a matchstatement in a physics_process function

Ex.

func _physics_process(delta):
	match state:
		"default":
			state_default()
		"attack":
			attack()
	
func state_default():
	print(health)
	damage_loop()
	follow_loop()
	spritedir_loop()
	if movedir!=Vector2(0,0):
		anim_switch("Walk")
	else:
		anim_switch("Idle")
	if distance<atkrange:
		state="attack1"

func attack():
	damage_loop()
	anim_switch("Attack")
	movedir=dir.Center

I hope this isnt too much for you
Its nothing really important just a hobby i do.

Newby | 2019-02-22 11:44