FPSInputController.js 1.4 KB

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