PlatformInputController.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // This makes the character turn to face the current movement speed per default.
  2. var autoRotate : boolean = true;
  3. var maxRotationSpeed : float = 360;
  4. private var motor : CharacterMotor;
  5. // Use this for initialization
  6. function Awake () {
  7. motor = GetComponent(CharacterMotor);
  8. }
  9. // Update is called once per frame
  10. function Update () {
  11. // Get the input vector from kayboard or analog stick
  12. var directionVector = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
  13. if (directionVector != Vector3.zero) {
  14. // Get the length of the directon vector and then normalize it
  15. // Dividing by the length is cheaper than normalizing when we already have the length anyway
  16. var directionLength = directionVector.magnitude;
  17. directionVector = directionVector / directionLength;
  18. // Make sure the length is no bigger than 1
  19. directionLength = Mathf.Min(1, directionLength);
  20. // Make the input vector more sensitive towards the extremes and less sensitive in the middle
  21. // This makes it easier to control slow speeds when using analog sticks
  22. directionLength = directionLength * directionLength;
  23. // Multiply the normalized direction vector by the modified length
  24. directionVector = directionVector * directionLength;
  25. }
  26. // Rotate the input vector into camera space so up is camera's up and right is camera's right
  27. directionVector = Camera.main.transform.rotation * directionVector;
  28. // Rotate input vector to be perpendicular to character's up vector
  29. var camToCharacterSpace = Quaternion.FromToRotation(-Camera.main.transform.forward, transform.up);
  30. directionVector = (camToCharacterSpace * directionVector);
  31. // Apply the direction to the CharacterMotor
  32. motor.inputMoveDirection = directionVector;
  33. motor.inputJump = Input.GetButton("Jump");
  34. // Set rotation to the move direction
  35. if (autoRotate && directionVector.sqrMagnitude > 0.01) {
  36. var newForward : Vector3 = ConstantSlerp(
  37. transform.forward,
  38. directionVector,
  39. maxRotationSpeed * Time.deltaTime
  40. );
  41. newForward = ProjectOntoPlane(newForward, transform.up);
  42. transform.rotation = Quaternion.LookRotation(newForward, transform.up);
  43. }
  44. }
  45. function ProjectOntoPlane (v : Vector3, normal : Vector3) {
  46. return v - Vector3.Project(v, normal);
  47. }
  48. function ConstantSlerp (from : Vector3, to : Vector3, angle : float) {
  49. var value : float = Mathf.Min(1, angle / Vector3.Angle(from, to));
  50. return Vector3.Slerp(from, to, value);
  51. }
  52. // Require a character controller to be attached to the same game object
  53. @script RequireComponent (CharacterMotor)
  54. @script AddComponentMenu ("Character/Platform Input Controller")