annotate Chapter5/Assets/SimpleCloudSystem by RM/SceneComponents/Standard Assets/Character Controllers/Sources/Scripts/FPSInputController.js @ 2:fdab88fc2cb9

add game projects
author Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2015 23:14:45 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 private var motor : CharacterMotor;
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // Use this for initialization
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 function Awake () {
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 motor = GetComponent(CharacterMotor);
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 }
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 // Update is called once per frame
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 function Update () {
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 // Get the input vector from kayboard or analog stick
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 if (directionVector != Vector3.zero) {
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 // Get the length of the directon vector and then normalize it
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 // Dividing by the length is cheaper than normalizing when we already have the length anyway
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 var directionLength = directionVector.magnitude;
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 directionVector = directionVector / directionLength;
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 // Make sure the length is no bigger than 1
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 directionLength = Mathf.Min(1, directionLength);
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 // Make the input vector more sensitive towards the extremes and less sensitive in the middle
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 // This makes it easier to control slow speeds when using analog sticks
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 directionLength = directionLength * directionLength;
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 // Multiply the normalized direction vector by the modified length
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 directionVector = directionVector * directionLength;
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 }
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 // Apply the direction to the CharacterMotor
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 motor.inputMoveDirection = transform.rotation * directionVector;
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 motor.inputJump = Input.GetButton("Jump");
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 }
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 // Require a character controller to be attached to the same game object
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 @script RequireComponent (CharacterMotor)
fdab88fc2cb9 add game projects
Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 @script AddComponentMenu ("Character/FPS Input Controller")