Running Animation 2D

Hi everyone,
I have a character that has a running animation.I have set up the “D” key for the forward movement.

This is my code:

void Movement()

{
anim.SetFloat(“speed”, Mathf.Abs(Input.GetAxis (“Horizontal”)));

if(Input.GetAxisRaw(“Horizontal”) > 0)
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0); //this sets the rotation of the gameobject
}

I want the player to start moving forward immediately i start the game without pressing down any key.Please Help:(

Using your current approach you could add a boolean value called gameStarted and set it equal to true like:

bool gameStarted = true;

Then within your if condition you could say:

if(Input.GetAxisRaw(“Horizontal”) > 0 || gameStarted)

Then I’m assuming you want it to stop once the user presses a key, so you would set the bool value to false at that time. So something like:

if(Input.GetAxisRaw(“Horizontal”) > 0)
gameStarted = false;

Immediately i start the game the player moves immediately,thankyou for that…The only problem is that the running animation is not playing

The player is moving but the animation is not working

Is the speed variable being sent to the animator and is the animator setup properly?

The setup is correct since when pressing the “D” key the running animation works perfectly

Try setting the animator parameter in your Start() function.

You could also call the animation directly by using the Play method on the transform’s animator. You could do this by creating a variable to store the animator, getting the animator component using the GetComponent method in the Start function, and then calling its Play method. This is kind of a hack approach though. I’d recommend using the parameter if possible.