2D character movement

I’m sure I messed up, I apologize in advance for the trivial question:
i’ve some problem with the animation of my character, and the respective script, i’ve set a boolean for play (the animation) with the “D” letter but the problem is that actually, if i press it again my character does not move a second time. the question is: using a Boolean in my code is actually the right choice? If it’s not, and this is the trivial part, i should insert rb2d.AddForce? When i used my animation didn’t play and my character does not move. Thanks in advance mates.

You can try with this :slight_smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {

    //[SerializeField]
    public float maxSpeed = 10f; // Maximum speed player can have

    //Facing of player. It can be Left or Right.
    bool facingRight = true; 

    // Rigidbody 2D component you need to add to Player GameObject
    private Rigidbody2D rb;

    private Animator animator;


	void Start ()
    {
        rb = GetComponent<Rigidbody2D>();
        animator.GetComponent<Animator>(); 
	}
	
	// It is better to do this things like movement in Fixed Update
	void FixedUpdate ()
    {
        ///<summary> You dont need if statement to check GetKeyDown(). GetAxix("Horizontal") will check if Player pressed keys (Left or Right Arrow, A or D).
        ///You can also change this in Input Settings if you want 
        ///</summary>
        ///
        float move = Input.GetAxis("Horizontal");

        rb.velocity = new Vector2(move * maxSpeed, rb.velocity.y);
        
        if(move > 0 && !facingRight) // Run Right
        {
            Flip();
            animator.Play("RunAnimationName");
        }
        else if (move<0 && facingRight) // Run Left
        {
            Flip();
            animator.Play("RunAnimationName");
        }

        else // Idle 
        {
            animator.Play("IdleAnimationName");
        }
    }
/// <summary>
/// This function Flip player Sprite Left or Right
/// </summary>
    void Flip()
    {
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }
}