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