DiveFPSController.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #pragma strict
  2. var inputMoveDirection:Vector3;
  3. var inputJump=false;
  4. var grounded=false;
  5. var downmovement=0.1;
  6. var velocity:Vector3;
  7. public var max_speed=0.1;
  8. public var max_speed_air=0.13;
  9. public var max_speed_ground=0.1;
  10. public var acceleration=10;
  11. public var acceleration_air=10;
  12. public var gravity=-0.18;
  13. public var friction=0.8;
  14. public var cameraObject:GameObject;
  15. private var controller : CharacterController;
  16. public var groundNormal:Vector3;
  17. public var jumpspeed=0.16;
  18. public var stopmovingup=false;
  19. public var fallkillspeed=-0.38;
  20. public var collisionFlags : CollisionFlags;
  21. public var ground_gameobject:GameObject=null;
  22. public var last_ground_pos:Vector3;
  23. private var jumpcommand=0;
  24. public var floating=false;
  25. public var autowalk:int=0;
  26. public var inhibit_autowalk=1;
  27. public var reload_once:int=0;
  28. function Awake () {
  29. controller = GetComponent (CharacterController);
  30. Debug.Log("Controller Slopelimit"+controller.slopeLimit);
  31. }
  32. function toggle_autowalk(){
  33. if (autowalk==0)autowalk=1;
  34. else autowalk=0;
  35. }
  36. function JumpUp(){
  37. jumpcommand=1;
  38. }
  39. function Start () {
  40. reload_once=0;
  41. }
  42. function OnControllerColliderHit (hit : ControllerColliderHit) {
  43. if (hit.normal.y > 0 && hit.moveDirection.y < 0) {
  44. groundNormal = hit.normal;
  45. grounded=true;
  46. ground_gameobject=hit.gameObject;
  47. //print("Landed on: ground"+ground_gameobject.name);
  48. stopmovingup=false;
  49. }
  50. }
  51. var fadeduration = 2.0; // fade duration in seconds
  52. function Die(){
  53. // create a GUITexture:
  54. var fade: GameObject = new GameObject();
  55. fade.AddComponent(GUITexture);
  56. // and set it to the screen dimensions:
  57. fade.GetComponent.<GUITexture>().pixelInset = Rect(0, 0, Screen.width, Screen.height);
  58. // set its texture to a black pixel:
  59. var tex = new Texture2D(1, 1);
  60. tex.SetPixel(0, 0, Color.black);
  61. tex.Apply();
  62. fade.GetComponent.<GUITexture>().texture = tex;
  63. // then fade it during duration seconds
  64. for (var alpha:float = 0.0; alpha < 1.0; ){
  65. alpha += Time.deltaTime / fadeduration;
  66. fade.GetComponent.<GUITexture>().color.a = alpha;
  67. yield;
  68. }
  69. // finally, reload the current level:
  70. if (reload_once==0){
  71. reload_once =1;
  72. var async : AsyncOperation = Application.LoadLevelAsync(Application.loadedLevel);
  73. yield async;
  74. }
  75. }
  76. function Update () {
  77. if (velocity.y < fallkillspeed)Die();
  78. Debug.DrawLine (transform.position, transform.position+groundNormal, Color.red);
  79. //print("GroundNormal y" +groundNormal.y);
  80. var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  81. if (autowalk==1)directionVector=Vector3(0,0,1*inhibit_autowalk);
  82. if (directionVector != Vector3.zero) {
  83. // Get the length of the directon vector and then normalize it
  84. // Dividing by the length is cheaper than normalizing when we already have the length anyway
  85. var directionLength = directionVector.magnitude;
  86. directionVector = directionVector / directionLength;
  87. // Make sure the length is no bigger than 1
  88. directionLength = Mathf.Min(1, directionLength);
  89. // Make the input vector more sensitive towards the extremes and less sensitive in the middle
  90. // This makes it easier to control slow speeds when using analog sticks
  91. directionLength = directionLength * directionLength;
  92. // Multiply the normalized direction vector by the modified length
  93. directionVector = directionVector * directionLength;
  94. }
  95. // Apply the direction to the CharacterMotor
  96. inputMoveDirection = directionVector;
  97. inputJump = Input.GetButton("Jump");
  98. if(jumpcommand==1){
  99. inputJump=true;
  100. jumpcommand=0;
  101. }
  102. grounded=(collisionFlags & CollisionFlags.Below);
  103. if (floating){
  104. velocity.y=0.1;
  105. }
  106. if (inputJump&&grounded){
  107. velocity.y+=jumpspeed;
  108. velocity.z*=1.5;
  109. //print ("Jump");
  110. grounded=false;
  111. }
  112. if (grounded){
  113. velocity+=inputMoveDirection*acceleration*Time.deltaTime;
  114. }
  115. else
  116. {
  117. velocity+=inputMoveDirection*acceleration_air*Time.deltaTime;
  118. }
  119. var translation=Vector3(velocity.x,0,velocity.z);
  120. translation=Vector3.Lerp(Vector3(0,0,0),translation,friction);
  121. velocity.x=translation.x;
  122. velocity.z=translation.z;
  123. velocity.y = velocity.y+gravity*Time.deltaTime;
  124. //print ("Time deltatime "+Time.deltaTime);
  125. if (grounded)velocity.y=0;
  126. //print ("Vel y "+velocity.y);
  127. if (grounded)max_speed=max_speed_ground;
  128. if (!grounded)max_speed=max_speed_air;
  129. if (translation.magnitude>max_speed){
  130. translation=translation/translation.magnitude;
  131. translation=translation*max_speed;
  132. }
  133. velocity.x=translation.x;
  134. velocity.z=translation.z;
  135. translation.y=velocity.y;
  136. var yrotation_camera= Quaternion.Euler(0, cameraObject.transform.rotation.eulerAngles.y, 0);
  137. //transform.position+=yrotation_camera*translation;
  138. var platformdelta:Vector3;
  139. if(ground_gameobject!=null) {
  140. //print("ground object ungleich null");
  141. platformdelta=ground_gameobject.transform.position-last_ground_pos;
  142. }
  143. //if (!grounded)platformdelta=Vector3.zero;
  144. //MAKE A MOVE!
  145. collisionFlags=controller.Move(yrotation_camera*translation+platformdelta);
  146. if (collisionFlags & CollisionFlags.CollidedAbove)
  147. {
  148. if (stopmovingup==false){
  149. print ("ControllerColliderHit");
  150. velocity.y=0;
  151. stopmovingup=true;
  152. }
  153. }
  154. if (ground_gameobject!=null && !grounded){
  155. ground_gameobject=null;
  156. }
  157. if(ground_gameobject!=null)last_ground_pos=ground_gameobject.transform.position;
  158. }
  159. function LateUpdate () {
  160. //groundNormal=Vector3.zero;
  161. }
  162. function OnGUI () {
  163. // GUI.Label (Rect (10, 10, 400, 20), "isgrounded "+grounded+ " velocity.y "+velocity.y+" collsionbelow "+ (collisionFlags & CollisionFlags.Below) );
  164. var e : Event = Event.current;
  165. if (e.isKey) {
  166. Debug.Log("Detected key code: " + e.keyCode);
  167. GUI.Label (Rect (10, 10, 400, 20), "Keycode ="+ e.keyCode );
  168. }
  169. }
  170. function OnTriggerEnter( other : Collider ) {
  171. if (other.name == "Float") {
  172. floating=true;
  173. inhibit_autowalk=0.1;
  174. }
  175. }
  176. function OnTriggerExit( other : Collider ) {
  177. if (other.name == "Float") {
  178. floating=false;
  179. inhibit_autowalk=1;
  180. }
  181. }
  182. @script RequireComponent (CharacterController)