hey there !
So I want to display an 8 direction sprite AI character.
My character follow a getsimplepath.
So far, for FOUR directions, i did this "check previous frame" > compare position> define the angle.
Looks like this :
var motion = position - _position_last_frame
if motion.length() > 0.0001:
#NSWE formula
_cardinal_direction = int(4.0 * (motion.rotated(PI / 4.0).angle() + PI) / TAU)
# And now you have it!
# You can even use it with an array since it's like an index
match _cardinal_direction:
0:
$AnimatedSprite.play("W")
1:
$AnimatedSprite.play("N")
2:
$AnimatedSprite.play("E")
3:
$AnimatedSprite.play("S")
# Remember our current position for next frame
_position_last_frame = position
So yeah this one works.
Assuming I want 8 directions i would go for this :
var motion = position - _position_last_frame
if motion.length() > 0.0001:
#NSWE formula
_cardinal_direction = int(8.0 * (motion.rotated(PI / 8.0).angle() + PI) / TAU)
# And now you have it!
# You can even use it with an array since it's like an index
match _cardinal_direction:
0:
$AnimatedSprite.play("N")
1:
$AnimatedSprite.play("NW")
2:
$AnimatedSprite.play("W")
3:
$AnimatedSprite.play("NE") # SOUTH
4:
$AnimatedSprite.play("E")
5:
$AnimatedSprite.play("SE")
6:
$AnimatedSprite.play("S")
7:
$AnimatedSprite.play("SW")
This one.... is giving me inconsistency. I must be doing something wrong.
2 questions...
1) Do you see a fix ? cause im getting clue less. Also I have no idea how to know which number matcher what direction... doing it manually...
2) Is there an other way you'd think of ?
Again, this is for AI, so i don't have any "input" to match.
Thanks !