2
|
1 private var motor : CharacterMotor;
|
|
2
|
|
3 // Use this for initialization
|
|
4 function Awake () {
|
|
5 motor = GetComponent(CharacterMotor);
|
|
6 }
|
|
7
|
|
8 // Update is called once per frame
|
|
9 function Update () {
|
|
10 // Get the input vector from kayboard or analog stick
|
|
11 var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
|
|
12
|
|
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
|
|
19 // Make sure the length is no bigger than 1
|
|
20 directionLength = Mathf.Min(1, directionLength);
|
|
21
|
|
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
|
|
26 // Multiply the normalized direction vector by the modified length
|
|
27 directionVector = directionVector * directionLength;
|
|
28 }
|
|
29
|
|
30 // Apply the direction to the CharacterMotor
|
|
31 motor.inputMoveDirection = transform.rotation * directionVector;
|
|
32 motor.inputJump = Input.GetButton("Jump");
|
|
33 }
|
|
34
|
|
35 // Require a character controller to be attached to the same game object
|
|
36 @script RequireComponent (CharacterMotor)
|
|
37 @script AddComponentMenu ("Character/FPS Input Controller")
|