/Scripts/ModifiedController.js

http://acid-and-base.googlecode.com/ · JavaScript · 79 lines · 51 code · 13 blank · 15 comment · 7 complexity · f30e606afcdc46a8025c6026c228e5fc MD5 · raw file

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