How can I flip not rotate - flip an enemy to the direction of the main player.

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

i want to know how to flip while following main player, the characters are isometric not top down

:bust_in_silhouette: Reply From: exuin

You can set the scale to -1 in order to flip the enemy. Calculate the angle between the enemy and the player character and then set the scale to -1 if the angle is greater than 180 degrees or whatever value you want.

This will only give you two directions that the enemy can face, though. If you want more, you would have to create more sprites for the enemy.

help with the code if possible

BEN_dev | 2021-01-13 10:01

I tried coding an answer to your problem, but then I realized that I have no clue what your sprites look like, so I wouldn’t know at what angle to flip them. What do your sprites look like?

exuin | 2021-01-13 19:04

hers the link to a a sample screenshot of my character

majorly i just want the enemy to face the relevant direction while walking towards the enemy . all the sprites for all the direction up,down.right and left are available

BEN_dev | 2021-01-13 19:25

Okay, since the sprite you linked (I don’t see it in your comment but it was in the email sent to me) is facing to the left, you want to flip it whenever the player character is 90+ degrees away from it (positive and negative 90). If the sprite was facing to the right, you’d want to do the opposite - flip it when it was between -90 and 90 degrees.

Here is the code (note that degrees are in radians)

func _physics_process(delta):
	var angle = global_position.angle_to_point(player.global_position)
	if abs(angle) > PI/2:
		scale.x = -1
		scale.x = 1

exuin | 2021-01-14 02:31