How can I make my character walk while pressing arrow keys?

Hi, so I made my character and a few animations to go along with it using Adobe Fuse and Mixamo. I’m having a few problems and after hours of searching for resolutions, I’m starting to go bananas! (literally)

Ok so enough sob story, let me tell you about my setup. I have an Animator Controller(AC) attached to my character GameObject and inside the AC there are two animations, an Idle which transitions from the entry, and a Walking animation. The idle seems to be working fine, but the Walking is where I get my problems. I want the player to be able to press the arrow keys and for the Walking animation to play while the key is being held down and once its released I want the animation to switch back to Idle. I currently have a script that controls my character movement but when I press the keys the Walking animation only play once, not repeatedly and it doesn’t go back to Idle.

Here is my c# script that’s attached to my character:

public class Harpermovement : MonoBehaviour {

public float speed = 2.0f;
public Animator anim;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
    anim = GetComponent<Animator>();

    if (Input.GetKey(KeyCode.RightArrow))
    {
        anim.Play("Walking");
        transform.position += Vector3.right * speed * Time.deltaTime;
    }
    if (Input.GetKey(KeyCode.LeftArrow))
    {
        anim.Play("Walking");
        transform.position += Vector3.left * speed * Time.deltaTime;
    }
    if (Input.GetKey(KeyCode.UpArrow))
    {
        anim.Play("Walking");
        transform.position += Vector3.forward * speed * Time.deltaTime;
    }
    if (Input.GetKey(KeyCode.DownArrow))
    {
        anim.Play("Walking");
        transform.position += Vector3.back * speed * Time.deltaTime;
    }
}

}

Also, if anyone knows how to switch the position my character is facing when I press the left and right arrow keys to walk that would be awesome. Currently when I walk her to the side her feet are still walking forward but she’s moving sideways :confused:

Ok so I will just assume you are making a 2D game for this answer, since you didnt provide any information.

First of all: This script is very very inefficient. It would be much smarter to try something like this:

using UnityEngine;
using System.Collections;

public class CharacterMover : MonoBehaviour
{

	public float speed;

	void Update ()
	{
		transform.Translate (Input.GetAxisRaw ("Horizontal") * speed, Input.GetAxisRaw ("Vertical") * speed, 0);
	}
}

Just assign “speed” and you should be good.

Now. How to change your animation? How about this:

Make animations for walking left, walking up, and walking down. Then controll these animations through bools and triggers.

If you are not familiar with them I would suggest you watch this tutorial:

http://unity3d.com/learn/tutorials/modules/beginner/animation/animator-scripting

Any more questions? Comment them :slight_smile:

I would add one thing that you’re missing and the other answer doesn’t mention: don’t forget to include Time.deltaTime when doing your movement calculations. Movement should be based on time elapsed, not on frames, and since Update() is called with every frame, it’s very likely that if you move the character a certain distance every frame, instead of every X.x seconds, you won’t get a consistent result across different devices. Time.deltaTime will give you the amount of time elapsed since the last frame was displayed, ie: the delta of time [elapsed].

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

public class MovementController : MonoBehaviour
{

public float movementSpeed = 50.0f; // The number of units you want to move
Vector2 movement = new Vector2();

 void Update()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");
        Vector2 newPosition = new Vector2(movement.x * movementSpeed * Time.deltaTime, movement.y * movementSpeed * Time.deltaTime);
        transform.Translate(newPosition);
    }
}