1. using UnityEngine;
    2. using System.Collections;
    3. [RequireComponent(typeof(CharacterMotor))]
    4. [AddComponentMenu("Character/FPS Input Controller")]
    5. public class FPSInputController : MonoBehaviour {
    6. private CharacterMotor motor ;
    7. // Use this for initialization
    8. void Awake () {
    9. motor = GetComponent<CharacterMotor>();
    10. }
    11. // Update is called once per frame
    12. void Update () {
    13. // Get the input vector from kayboard or analog stick
    14. Vector3 directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    15. if (directionVector != Vector3.zero) {
    16. // Get the length of the directon vector and then normalize it
    17. // Dividing by the length is cheaper than normalizing when we already have the length anyway
    18. var directionLength = directionVector.magnitude;
    19. directionVector = directionVector / directionLength;
    20. // Make sure the length is no bigger than 1
    21. directionLength = Mathf.Min(1, directionLength);
    22. // Make the input vector more sensitive towards the extremes and less sensitive in the middle
    23. // This makes it easier to control slow speeds when using analog sticks
    24. directionLength = directionLength * directionLength;
    25. // Multiply the normalized direction vector by the modified length
    26. directionVector = directionVector * directionLength;
    27. }
    28. // Apply the direction to the CharacterMotor
    29. motor.inputMoveDirection = transform.rotation * directionVector;
    30. motor.inputJump = Input.GetButton("Jump");
    31. }
    32. }