We can use the left-right arrow keys (or A and D) to turn by adding a few more lines to our script:
- We can add a new public variable for the rotation speed: rotateSpeed.
- We can add a new private variable, horizontal, and set its value in Update() based on the Input.GetAxis(...) method.
- We can then perform a Rotate(...) operation on the character's transform component in FixedUpdate():
using UnityEngine;
public class PlayerMoveTurn : MonoBehaviour {
public float rotateSpeed = 1;
private float vertical = 0;
private float horizontal = 0;
private bool runKeyDown = false;
private Animator animator;
private void Awake() {
animator = GetComponent<Animator>();
}
private void Update() {
vertical = Input.GetAxis("Vertical");
horizontal = Input.GetAxis("Horizontal");
runKeyDown = Input.GetKey(KeyCode.LeftShift) ||
Input.GetKey(KeyCode.RightShift);
}
void FixedUpdate...