hi, I am very knew to unity and do not know many functions. so i have a code in visual studio to control my animations and movements, like Walking, running and jumping.
I now want to make my charachter crouch while i press “s” in the game.
I started to set up the transitions in the animator but all i can get is crouching without standing back up afterwards…
Any help is appreciated !
here is my code to date:
public class ControleurRobotboy : MonoBehaviour
{
public float vitesse; //vitesse déplacements
public float impulsion; // saut
float deplacement; //input
bool saute; //état de saut
int nbSauts; // gestion double sauts
Rigidbody2D rb; // \
SpriteRenderer sr; // > efficacité
Animator anim; // /
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
sr = gameObject.GetComponent<SpriteRenderer>();
anim = gameObject.GetComponent<Animator>();
deplacement = 0;
saute = false;
nbSauts = 0;
}
void Update()
{
deplacement = Input.GetAxis("Horizontal"); //input du clavier -1 +1
anim.SetFloat("Etat", Mathf.Abs(deplacement));
// direction du robot
if (deplacement < 0)
{
sr.flipX = true;
}
if(deplacement > 0)
{
sr.flipX = false;
}
// gestion de saut
if(Input.GetKeyDown("w") && nbSauts < 2)
{
anim.SetTrigger("Saut");
saute = true;
nbSauts++;
}
}
private void FixedUpdate()
{
rb.AddRelativeForce(Vector2.right * vitesse * deplacement);
if(saute)
{
rb.AddRelativeForce(Vector2.up * impulsion, ForceMode2D.Impulse);
saute = false;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
nbSauts = 0;
}
}