Running character in 2D RPG help

Here is my cube:

It moves diagonally, up, left, right, and down. However, I would really like to add in running to this script. I want to grab the current direction in which the object is traveling and multiply the speed by two.

using UnityEngine;
using System.Collections;

public class CharacterController : MonoBehaviour
{
    public float moveSpeed = 1f;

    void FixedUpdate()
    {
        if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(Vector3.up * moveSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.A))
        {
            transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.S))
        {
            transform.Translate(Vector3.down * moveSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.D))
        {
            transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
        }
    }
}

Thanks! (P.S. This is my first post!)

Can you be more specific? Do you want a “run” button that when held makes the transform move faster in the direction it’s heading?

Yeah, exactly like that.

Well, just write a boolean statement then. I’d suggest something like this. If the script doesn’t work, just reply about it. I’m happy to help, but I couldn’t test it.

EDIT: rename it to CharacterController.cs. OOPS

2656264–187256–moving_character.cs (1.27 KB)

You should be polling input values and Translating your character in Update. FixedUpdate is for physics updates.

Basically every frame you’ll have to look for this input value and select the speed based on it. I prefer this syntax:

moveSpeed = Input.GetKey(KeyCode.LeftShift) ? runSpeed : walkSpeed;