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]
Basically you are still using the same positive and negative axis directions with your horizontal axis, and translating them into local space, but they're still the same direction. Thus, use its forward axis, which you're already flipping by turning it, so you don't need the horizontal axis' positive or negative sign.
– Alec-Slayden