# Moving and making a character look in the same direction he's moving to

Hello, I am very new at unity and here is my problem.
I have an object I would like to move and have him face the direction I am moving to. So or example, I press the left arrow key my character immediately looks at the left and subsquently moves to that direction. The same logic applies to the other directions.
The following code works but the issue here is that it also rotates the axis he is on, as in I press left and the object looks at the left direction but moves “up” instead of going to the left side of the screen. Here just a snippet of what of what I have so far.

``````float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");

if (Input.GetKeyDown (KeyCode.S) || Input.GetKeyDown (KeyCode.DownArrow))
transform.eulerAngles =
new Vector3 (transform.eulerAngles.x, 180.0f, transform.eulerAngles.z);

transform.Translate(horizontal, vertical, 0);
``````

Try

``````float speed = 5.0f;

void Update() {

float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");

transform.Translate((new Vector3(horizontal, 0, -vertical).normalized) * speed * Time.deltaTime, Space.World);
transform.eulerAngles = new Vector3(0, Mathf.Atan2(horizontal, -vertical) * Mathf.Rad2Deg, 0);
}
``````

for moving down on the screen, considering that the camera is above and “up” on the screen is +z and so forth.

Here is an excellent tutorial:

Unity 3.x Scripting-Character Controller versus Rigidbody

You will have collision problems if you don’t apply this through the CharacterController’s Move() function. Here’s one for moving relative to the camera.

``````public CharacterController charController;
public float moveSpeed = 1f;
public float gravityForce = 9.8f;

void Update()
{
// NOTES

float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");

// Get forward (relative to cam)
Vector3 forward = Camera.main.transform.TransformDirection(Vector3.forward);
forward.y = 0f;
forward = forward.normalized;

// Get right
Vector3 right = new Vector3(forward.z, 0f, -forward.x);

// Make a local move direction (right + forward)
Vector3 localMoveDir = (horizontal * right + vertical * forward);

// Turn to face, smooth
if (localMoveDir != Vector3.zero)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(localMoveDir), 10f * Time.smoothDeltaTime);
transform.eulerAngles = new Vector3(0f, transform.eulerAngles.y, 0f);
}

// Diagonal speed clamp
if (localMoveDir.sqrMagnitude > 1f)
localMoveDir = localMoveDir.normalized;

// Gravity
localMoveDir.y = (Physics.gravity * gravityForce * Time.deltaTime).y;

// APPLY MOVE
charController.Move(localMoveDir * moveSpeed * Time.deltaTime);
}
``````

If you want him to move along the regular world axis, then modify the line to this:

``````// Make a local move direction
Vector3 localMoveDir = new Vector3(horizontal, 0f, vertical);
``````