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?
using UnityEngine;
using System.Collections;
public class Move : MonoBehaviour{
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3(0, 0, Input.GetAxis("Horizontal"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
I tried this
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 turning direction is fixed but it still moves in only one direction.
[Edit Berenger : Code formatting]