How can I alternate states of animation with user Input (AnimatedSprite3D) [!!!!!ANSWERED!!!]

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

Hi I’ve trouble with making my Sprite (3D) altenate between an “Idle” animation and a “Walking” animation.

I was able to find a page where this question was also asked ----- ( https://godotforums.org/discussion/20361/how-do-i-play-an-animatedsprite3d-when-i-am-moving ) ------ ,

but it was ment for a node that I’ve never heard of ----- ( I am talking about AnimatedSprite3D node ) ------.

I tried looking it up to see if there were any tutorials on this particualar node, but there wasn’t anyone talking about it, but I do need it to make my Sprite move in the 3D enviroment.

In the AnimatedSprite3D node you use frames from your spritesheet to make a quick animation of your character without doing the work around of a Sprite3D node and AnimationPlayer. (this is my intrepertation soo this might be wrong).

So what have I done:

  • In the AnimatedSprite3D I made an “Idle” animation in 8 directions named “Idle_F, Idle_FL, Idle_FR, Idle_B, Idle_BL, Idle_BR, Idle_L, Idle_R” -------------------- F = Forward, B= Backward, L = Left and R = Right -----------------

  • I did the same thing with the “Walking” animation and named it in the same style as the “Idle” one. (“Walking_F, Walking_B, etc.”)

  • In case you haven’t clicked on the link, the coding is as follows:

extends AnimatedSprite3D
var moving_anim_name = “moving”;
var idle_anim_name = “idle”;
var velocity = Vector3(0,0,0);
var move_speed = 20;

func _ready():
play(idle_anim_name);

func _process(delta):
if (Input.is_action_pressed(“ui_right”)):
velocity.x = move_speed;
elif (Input.is_action_pressed(“ui_left”)):
velocity.x = -move_speed;
else:
velocity.x = 0;
if (Input.is_action_pressed(“ui_up”)):
velocity.z = move_speed;
elif (Input.is_action_pressed(“ui_down”)):
velocity.z = -move_speed;
else:
velocity.z = 0;
global_translate(velocity * delta)
var h_vel = velocity;
h_vel.y = 0;
if (h_vel.length() > 0):
if (animation != moving_anim_name):
play(moving_anim_name);
else:
if (animation != idle_anim_name):
play(idle_anim_name);

I have changed the coding to this:

extends AnimatedSprite3D

var moving_anim_name = “Walking_F”;
var idle_anim_name = “Idle_F”;
var velocity = Vector3(0,0,0);
var move_speed = 2;

func _ready():
play(“Idle_F”);

func _process(delta):
if (Input.is_action_pressed(“Walk_Right”)):
velocity.x += move_speed;
elif (Input.is_action_pressed(“Walk_Left”)):
velocity.x -= move_speed;
else:
velocity.x = 0;

if (Input.is_action_pressed(“Walk_Forward”)):
velocity.z -= move_speed;
elif (Input.is_action_pressed(“Walk_Backward”)):
velocity.z += move_speed;
else:
velocity.z = 0;

global_translate(velocity * delta)

var h_vel = velocity;
h_vel.y = 0;
if (h_vel.length() > 0):
if (animation != moving_anim_name):
play(“Walking_F”);
else:
if(animation != idle_anim_name):
play(“Idle_F”);``

What I see now is as follows after making the animations and code

Left Right Up Down play the Animation of " walking forward (Walking_F) " this is good but also bad because

Left Right and Down don’t show the correct animation

also the character keeps accelerating and proceeds to slide in the same direction, when pressing the opposite direction or just stopping.

After all this what are my questions?:

  1. How do I use the AnimatedSprite3D properly

  2. What do I need to do to make my characters’ animation work in all 8 directions? So when the player presses Up(“W”) the character will Walk Forward + animation. This also needs to happen for the “Forward Left”, “Forward Right”, “Backward Right” and “Backward Left”

  3. Lastly if you were also making a 2D sprite in a 3D world game and already figured another way to display the animation in all 8 directions. Could you please explain or show it to me?

Thanks for reading this post,

Kellin (KindoSaur_Productions)

PS: if you have the coding for only the Sprite3D than that wil be alright as well I just want to be able to switch between my animations during the user input moments

:bust_in_silhouette: Reply From: KindoSaurProductions

https://godotforums.org/discussion/21344/how-can-i-alternate-between-animations-during-user-input-with-a-animated-sprite3d-node#earliest

You need to scroll down before you can see the entire code
it was written on October 14th