How can I adjust the direction of my animation based on the angle?

Godot Version

Godot 4 2D top down


When I do the shoot command, I want better animations, so I want to with each angle created by this command:

var shootDegree = rad_to_deg(position.angle_to_point(targetPosition))

I have divided the circle into parts as shown corresponding to the animations I will add

So how can I do that? Does it work with animated trees?

I assume your character will not always be looking where it is aiming, and you just need a temporary angle for the animation?

You may just need a state machine in the animation tree that can jump into your fire animation 2d blend node.

Also the angles can be described as this with a +/- π/8 margin on either side.

These are the center angles in radians for each of the 8 slices.
Right, up right, up, up left, left,
0, π/4, π/2, 3π/4, π,
Left down, down, right down, ( right again)
5π/4, 3π/2, 7π/4, (2π == 0)

You can also use a dot product to find the angle relationship between two vectors

I have adjusted my blendspace2D as shown with the points being animated accordingly. But blendspace2D only allows receiving vector values ​​(x,y).

I don’t really understand you very well, I saw a video tutorial and they just used code to calculate the conditions relative to the angle the function returns.

var shootDegree = rad_to_deg(position.angle_to_point(targetPosition))
if shootDegree < 135 and shootDegress > -45:
elif ....

Is there any way? Is there a way to convert angles to vectors?

Yea, UR (0.7071, 0.7071) (or √(2)/2 ) this is the vector for 45° for up right on a unit circle. You just need to change the signs for the other three. UL(-,+), DL(-,-), DR(+,-)

(BTW a 45° vector on a unit square is just (1,1), but a length of 1.414. The a unit circle is better because the length of the vector is always one, )

If you want to know more this all relates to the unit circle and trigonometry.

1 Like