Rotate to facing direction

Does anyone know how to get a character to rotate and move in the direction of the key they press?

Like pressing the left key makes the character look left and move that way?

I need it for a 2.5D game.

Create a empty player object and place the below script on it, then put the playergraphics and animations in a child object of player and drag it into the inspector.
This is in C#(csharp).

public class Player : MonoBehaviour {

private float speed = 1.5f;
public Transform playergraphic;

void  Update (){
Movement();
}

void  Movement (){

		//Player object movement
float horMovement = Input.GetAxisRaw("Horizontal");
float vertMovement = Input.GetAxisRaw("Vertical");
if(horMovement != 0 && vertMovement != 0){
speed = 1.0f;
}else{
speed = 1.5f;
}
transform.Translate(transform.right * horMovement * Time.deltaTime * speed);
transform.Translate(transform.forward * vertMovement * Time.deltaTime * speed);

		//Player graphic rotation
Vector3 moveDirection= new Vector3 (horMovement, 0, vertMovement);	
if (moveDirection != Vector3.zero){
Quaternion newRotation = Quaternion.LookRotation(moveDirection);
playergraphic.transform.rotation = Quaternion.Slerp(playergraphic.transform.rotation, newRotation, Time.deltaTime * 8);
}
		//Player graphic animation
if(moveDirection.magnitude > 0.05f){
playergraphic.animation.CrossFade("walk", 0.2f);
}else{
playergraphic.animation.CrossFade("idle", 0.2f);
}
}
}
transform.forward = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")));

Edit in response to comment

The transform.forward vector is the "forward" orientation of the transform (the positive Z direction in local space, aka object space). So, when we assign & change this direction, Unity will automatically rotate your object so that its Z axis is the same as our supplied forward vector.

The GetAxis() calls return a value -1 to 1 depending on where the input is positioned along that axis. On the horizontal, -1 is left and 1 is right. On the vertical, 1 is up and -1 is down. So here, new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")), the left-right keys will make the X component of the vector -1 and 1 (respectively), and similarly with the Z axis and the up-down keys.

So the code will give you 8 directions (in 45-degree increments). The default value, when all input is 0, is not defined explicitly.

I was in a rush when I typed that, but a better and much easier solution is only updating when an edge is encountered, for example:

 if (Input.GetKeyDown(KeyCode.W))
        transform.forward = new Vector3(0f, 0f, 1f);
    else if (Input.GetKeyDown(KeyCode.S))
        transform.forward = new Vector3(0f, 0f, -1f);
    else if (Input.GetKeyDown(KeyCode.A))
        transform.forward = new Vector3(1f, 0f, 0f);
    else if (Input.GetKeyDown(KeyCode.D))
        transform.forward = new Vector3(-1f, 0f, 0f);

The GetKeyDown function will return true when the key goes from being released to pressed (when the user starts pressing the key). Something like that should work for 4 directions and I think requires no explanation.

You could give this a try…

float faceDirection = Input.GetAxisRaw("Horizontal");
	if(faceDirection != 0)
	{
		transform.forward = new Vector3(faceDirection, 0, 0);
	}

This is just to rotate the character in the proper direction.
To move him afterwards, use a

controller.Move(moveDirection * Time.deltaTime)

function. This works for me, and I’m making a 2.5D Platformer

I am new to unity, but I believe there is an example of this in Standard Assets ThirdPersonController script. I also think there is another example in the platformer tutorial file. http://unity3d.com/support/resources/tutorials/3d-platform-game

I don’t know if this is “good” code, but it seems to work:

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour {
    bool facingRight;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
       if (Input.GetKeyDown (KeyCode.A)) {
         transform.right = new Vector3(-1, 0, 0);
         facingRight = false;
       }
       else if (Input.GetKeyDown (KeyCode.D)) {
         transform.right = new Vector3(1, 0, 0);
         facingRight = true;
       }
       float translate = Input.GetAxis("Horizontal");

       if(!facingRight)
         transform.Translate (Vector3.right * -translate * 5 * Time.deltaTime);
       else
         transform.Translate (Vector3.right * translate * 5 * Time.deltaTime);
    }
}

Hey Guys! It’s simple! Here is what i did. It took a while though, and after finding this page, i got to work and got my oritentation problem fixed! here is the code. note that it’s for rigidbody type control, but it should be easy to replace the rigidbody.velocity calculation with a similar one for character controllers:

if (Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")){
		//animation.Play("Walk");
		isMoving = true;
	}
	else {
		//animation.Play("Idle");
		isMoving = false;
	}
	var x = -Input.GetAxisRaw("Horizontal") * walkSpeed;
	var y = -Input.GetAxisRaw("Vertical") * walkSpeed;
	var previousVector : Vector3;
	
	if (isMoving){
		transform.forward = Vector3.Normalize(Vector3(-Input.GetAxis("Horizontal"), 0f, -Input.GetAxis("Vertical")));
		previousVector = Vector3.Normalize(Vector3(-Input.GetAxis("Horizontal"), 0f, -Input.GetAxis("Vertical")));
	}
	
	if (transform == Vector3.zero && !isMoving) {
		transform.forward = transform.TransformDirection(previousVector);
	}

it should also be easy to make the x and y variables one to use across all the script so you can then use the coordinates to move your character. to do this, simply use a vector3 with the x and z coordinates set to the x and y variables, and the y coordinate for whatever you calculate for gravity, like this:

<variable for direction of movement> = Vector3(x, <gravity velocity>, y);

There you go. my two cents!

-FuzzyQuills