#Direction-dependent 8-directional 2D animation
51 messages · Page 1 of 1 (latest)
assuming your character is a CharacterBody2D:
var angle = velocity.angle()
# substract 1/8 turn
angle -= PI / 4
# convert from radians to [0, 1]
angle /= PI * 2
# map to [0, 7] rounding down
var direction = floor(
8 * angle
)
# play the corresponding animation
var animation_name = "walking_" + direction
$AnimatedSprite2D.play(animation_name)
What do you mean by dependent on the current direction?
In a case where you're using a kinematic body to calculate a velocity, you can prob use that and the step function on the velocity's angle to control your animation.
In a case (like Undertale) where you have a constant movement speed, with no friction or acceleration, your input will always be your walking direction.
Either way, I'd love to help but I don't know what exactly you're looking for ^^; examples would help a lot obv
(also I'd provide lil code snippets but am in bed and super sleepy lol)
What I meant is basically in undertale while you move right you can press 'up' and it won't immediately switch the animation to 'up' for you are still mainly going right (if that makes any more sense?)
The same works with any direction
So it's like not really Input driven but rather main direction driven
And I have no idea how to implement that
I'll try to rephrase so I can understand you better ^^
You want to not be able to override the current movement direction
Yes
Thank you
So how do I do that? Could you please explain?
i know exactly what ur talking abt
i can show u the code i use its probably bad but the only thing i could get to work
Okay
Please do!
oh wait ur doing 8way
my issue was that i was doing 8 way movement with only 4 way animations
a la dr
ah yeah but I also use only 4 anims
oh ok
i would just check if any new input direction was more than 90 degrees off from the current one
That actually sounds pretty good
Could you show the code? I'm like a beginner lol
trying to find code
yeah
Alright
if new_dir == Vector2.ZERO:
return
if new_dir == p.facing:
return
if new_dir.length() > 1:
if rad_to_deg(abs(new_dir.angle_to(p.facing))) > 90:
p.facing.x = new_dir.x
return
p.facing = new_dir
```
ok its kinda fucked u p but i can explain it
i put it in a separate function so i could move it far away and not look at it
basically new_dir is just the direction of ur input.like what arrow keys ur pressing
facing is a vector2 that points toward the direction the sprite is facing.for visual purposes
so in the function i just check if its Not zero, and if its Not the same as it already was, and then i check if its diagonal
and if its diagonal, i have to check to see if ur actually like totally changing direction
90 angle change
and if so, i chang the x value i dont rly remember why
I don't really fully understand it but that's like REALLY useful
Thank you
well im gonna redo it p soon and try and make it make sense but i mglad it helped
:3
ok I made a crude but simpler to read way
var xaxis = Input.get_axis("SteerRight", "SteerLeft")
var yaxis = Input.get_axis("Reverse", "Accelerate")
print(dir)
if(Input.is_action_just_released("Accelerate") ||
Input.is_action_just_released("Reverse") ||
Input.is_action_just_released("SteerLeft") ||
Input.is_action_just_released("SteerRight")):
dir = Vector2.ZERO
if(abs(xaxis) > 0):
dir = xaxis * Vector2.RIGHT
if (abs(yaxis) > 0):
dir = yaxis * Vector2.UP
```
Input.get_axis returns a value that is either 1, 0, or -1
The only thing is that left and right can overwrite up and down
but up and down and overwrite left and right
if input != Vector2.ZERO and abs(input.angle_to(parent.sprite_facing)) > PI/2.0:
sprite_facing = input
#in case u do a frame perfect diagonal.just prefer horizontal sprites
if input.x != 0 and input.y != 0:
sprite_facing = Vector2(round(input.x), 0)
i know this is so old but this is the code i use now
i dont know how i got it to be so many lines the first time bro
You probably want to normalize that