Hello hello ! I'm still quite new to Godot and struggling to animate my character correctly. I've managed to add basic movements such as crouching, jumping, and walking, but I can't seem to add a roll or attacks. My main problem is that I don't know how to execute only one iteration of an animation. I've tried various tactics, but none have worked for me, either because I didn't implement them correctly or they weren't the right solution. Either way, I hope somebody can help me with it. I'll put my code right below, and don't hesitate to ask me some questions about my nodes or naming if you struggle to understand or if it's necessary.

using Godot;
using System;

public partial class personnage : CharacterBody2D
{
	
	
	public const float Speed = 300.0f;
	public const float JumpVelocity = -300.0f;
	string anim ="";

	AnimatedSprite2D animation;
	//AudioStreamPlayer audio;
	public float gravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle();
	private Vector2 velocity;
	
	public override void _Ready()
	{
		// Assurez-vous que le nom de la node correspond exactement au nom de la node dans votre scène
		animation = GetNode<AnimatedSprite2D>("animation");
		//audio = GetNode<AudioStreamPlayer>("audio");
	}

	public override void _PhysicsProcess(double delta)	
	{
		//aerien -- jump -- fall
		//Input.GetActionStrength("vertical")
		velocity = Velocity;

		if (!IsOnFloor()){
			velocity.Y += gravity * (float)delta;
			if(velocity.Y<-15){
				animation.Play("startjump");
				anim="startjump";
			}
			if(velocity.Y>15){
				animation.Play("endjump");
				anim="endjump";
			}
			if(velocity.Y<15 && velocity.Y>-15){
				animation.Play("peakjump");
				anim="peakjump";
			}
		}

		// if (Input.IsActionJustPressed("jump") && IsOnFloor()){;
		// 	velocity.Y = JumpVelocity;
		// }

		//attaque -- neutre -- crouch
		if(Input.IsActionJustPressed("attack") && IsOnFloor() && anim != "roll" && anim != "slide"){
			animation.Play("attack");
			anim="attack";
		}
		
		if(Input.IsActionJustPressed("attack") && IsOnFloor() && anim != "roll" && anim != "slide" && Input.IsActionJustPressed("crouch")){
			animation.Play("crouchattack");
			anim="crouchattack";
		}
		
		//sol -- gauche -- droite -- crouch
		float mouvementhorizontal = Input.GetAxis("gauche", "droite");
		if (mouvementhorizontal != 0 && IsOnFloor() && anim != "attack" && anim != "roll" && anim != "slide")
		{
			if(mouvementhorizontal>0){
				animation.FlipH = false;
				if(Input.IsActionPressed("crouch")){
				animation.Play("crouchwalk");
				anim="crouchwalk";
				velocity.X = mouvementhorizontal * Speed - 150;
				}else{
					animation.FlipH = false;
					animation.Play("walk");
					anim="walk";
					velocity.X = mouvementhorizontal * Speed;
				}
			}
			else{
				animation.FlipH = true;
				if(Input.IsActionPressed("crouch")){
					animation.Play("crouchwalk");
					anim="crouchwalk";
					velocity.X = mouvementhorizontal * Speed + 150;
				}else{
					animation.FlipH = true;
					animation.Play("walk");
					anim="walk";
					velocity.X = mouvementhorizontal * Speed;
				}
			}
		}else if(mouvementhorizontal == 0 && IsOnFloor() && anim != "attack" && anim != "roll" && anim != "slide" ){
			animation.Play("idle");
			anim="idle";
			if(Input.IsActionPressed("crouch") && mouvementhorizontal == 0){
			animation.Play("crouchidle");
			audio.Play();
			anim="crouchidle";
			}
			velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
		}
		// roll -- slide
		if(Input.IsActionJustPressed("roll") && IsOnFloor()){
			//acceleration puis decceleration
			animation.Play("roll");
			anim="roll";
		}
		if(Input.IsActionJustPressed("slide") && IsOnFloor()){
			//vitesse normale puis acceleration
			//et boost si jump pour le cancel
			animation.Play("slide");
			anim="slide";
		}
		Velocity = velocity;
		MoveAndSlide();

	}
	
}

    CARIBOU81 mmhh
    1 - use an AnimationTree for controlling this number of animations. use an AnimationTree StateMachine to set attack, roll, etc states.
    put the movement in one of the states, make it a blendtree, there you can put more combinations of animations.
    2 - your animations (attack, roll) are being RESET by the play() methods inside each of the gazillion if/elses. STOP USING C#. you are clearly a beginner in coding, you will get more help if you code in gdscript and it will be easier and also force you to write better code.
    3 - you NEED state machines, you need to start an attack state and only stop when the animation ends or time finishes, and then resume the rest of the movement code, otherwise you need animations that combine attack and movement.
    4 - you need to put physical movement and animation separately.
    5 - and use else and elif. just look at this:

    if(velocity.Y<-15){
    	animation.Play("startjump");
    	anim="startjump";
    }
    if(velocity.Y>15){
    	animation.Play("endjump");
    	anim="endjump";
    }
    if(velocity.Y<15 && velocity.Y>-15){
    	animation.Play("peakjump");
    	anim="peakjump";
    }

    you are evaluating the first if, then the second, then the third, all three.
    is the last one testing if velocity... you can replace that with an else...
    this is what this part of the code should look like:

    if(velocity.Y<15 && velocity.Y>-15)
    {
    	animation.Play("peakjump");
    	anim="peakjump";
    }
    else if(velocity.Y<-15)
    {
    	animation.Play("startjump");
    	anim="startjump";
    }
    else
    {
    	animation.Play("endjump");
    	anim="endjump";
    }
    9 days later

    hey thank you for the advice will use it wisely (i hope at least) as for the languague used am not gonna change it even if i agree with you for 2 reasons, first i find it more usefull to learn a language used elsewere rather than just in godot and second we are 3 friends on the project and the others also wish to work with c#.

    on top of that its a litle side project so we are not in any rush.

    as for the animation tree i've seen it but didnt understood it clearly so tried something alse but i'll look back into it.

    ill also try to think about my strucutre with my if and elses a bit more.

    overall thanks for the advices 🙂.

      CARIBOU81 for the language used am not gonna change it even if i agree with you

      CARIBOU81 first i find it more usefull to learn a language used elsewere rather than just in godot

      you are like a toddler trying to design the space shuttle.
      you have to learn how to walk before you try to run.
      first make something simple that works, then you can think of adding more features. and you are going to fail many times, you have to learn to fail, abandon your project and start over, and learn from your mistakes. you won't make an MMORPG as your first game.

      The first language I tried was c, and it didn't work out. I only learned programming after working with python, then going to C++ was much easier, and from there learning C# took me months, and gdscript took me a week.

      I refused gdscript back when I was on python because it wasn't used anywhere else, but I have changed my thinking since, and that's because I have a better understanding of what takes to code.
      The language you code in doesn't matter, what matters is learning how to code. and for that something as simple as gdscript will teach you the basics, think of it like training wheels.
      the code you posted may be valid, it has correct "code syntax", but if you don't know how to use conditions, it is never going to do what you want it to do.

      C# in Godot is ADVANCED, it's not like C# in unity, you have to work with something that is designed with gdscript in mind, and the only advantage is performance, but you should only care about this on a large scale project, and once you have a good understanding of the engine and know what you are doing.

      CARIBOU81 we are 3 friends on the project and the others also wish to work with c#.

      Does anyone in your team know how to code?

      CARIBOU81 as for the animation tree i've seen it but didnt understood it clearly so tried something alse but i'll look back into it.

      you should slow down and take the time to learn the tools you need to use and the engine.

        Jesusemora its harsh but its true. the esasiest way to improve is to start over. Why do you think AI advanced this fast? because AI tried to achieve the same thing using different approaches. it just faster at that because its a machine 😃