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();
}
}