Guest User

Untitled

a guest
Apr 25th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. /*
  2. animations played are:
  3. idle, threaten, turnjump, attackrun
  4. */
  5.  
  6. var attackTurnTime = 0.7;
  7. var rotateSpeed = 120.0;
  8. var attackDistance = 17.0;
  9. var extraRunTime = 2.0;
  10. var damage = 1;
  11.  
  12. var attackSpeed = 5.0;
  13. var attackRotateSpeed = 20.0;
  14.  
  15. var idleTime = 0.5;
  16.  
  17. var punchPosition = new Vector3 (0.4, 0, 0.7);
  18. var punchRadius = 1.1;
  19.  
  20. // sounds
  21. var idleSound : AudioClip; // played during "idle" state.
  22. var attackSound : AudioClip; // played during the seek and attack modes.
  23.  
  24. private var attackAngle = 10.0;
  25. private var isAttacking = true;
  26. private var lastPunchTime = 0.0;
  27.  
  28. var target : Transform;
  29.  
  30. // Cache a reference to the controller
  31. private var characterController : CharacterController;
  32. characterController = GetComponent(CharacterController);
  33. private var police_attack_upon_true : InventoryManager;
  34.  
  35. // Cache a link to LevelStatus state machine script:
  36. var levelStateMachine : LevelStatus;
  37.  
  38. function Start ()
  39. {
  40. levelStateMachine = GameObject.Find("/Level").GetComponent(LevelStatus);
  41.  
  42. if (!levelStateMachine)
  43. {
  44. Debug.Log("EnemyPoliceGuy: ERROR! NO LEVEL STATUS SCRIPT FOUND.");
  45. }
  46.  
  47. if (!target)
  48. target = GameObject.FindWithTag("Player").transform;
  49.  
  50. animation.wrapMode = WrapMode.Loop;
  51.  
  52. // Setup animations
  53. animation.Play("idle");
  54. animation["threaten"].wrapMode = WrapMode.Once;
  55. animation["turnjump"].wrapMode = WrapMode.Once;
  56. animation["gothit"].wrapMode = WrapMode.Once;
  57. animation["gothit"].layer = 1;
  58.  
  59. // initialize audio clip. Make sure it's set to the "idle" sound.
  60. audio.clip = idleSound;
  61.  
  62. yield WaitForSeconds(Random.value);
  63.  
  64. // Just attack for now
  65. while (true)
  66. {
  67. // Don't do anything when idle. And wait for player to be in range!
  68. // This is the perfect time for the player to attack us
  69. yield Idle();
  70.  
  71. // Prepare, turn to player and attack him
  72. yield Attack();
  73. }
  74. }
  75.  
  76.  
  77. function Idle ()
  78. {
  79. // if idling sound isn't already set up, set it and start it playing.
  80. if (idleSound)
  81. {
  82. if (audio.clip != idleSound)
  83. {
  84. audio.Stop();
  85. audio.clip = idleSound;
  86. audio.loop = true;
  87. audio.Play(); // play the idle sound.
  88. }
  89. }
  90.  
  91. // Don't do anything when idle
  92. // The perfect time for the player to attack us
  93. yield WaitForSeconds(idleTime);
  94.  
  95. // And if the player is really far away.
  96. // We just idle around until he comes back
  97. // unless we're dying, in which case we just keep idling.
  98. while (true)
  99. {
  100. characterController.SimpleMove(Vector3.zero);
  101. yield WaitForSeconds(1.0);
  102.  
  103. var offset = transform.position - target.position;
  104.  
  105. // if player is in range again, stop lazyness
  106. // Good Hunting!
  107. if (offset.magnitude < attackDistance)
  108. return;
  109. }
  110. }
  111.  
  112. function RotateTowardsPosition (targetPos : Vector3, rotateSpeed : float) : float
  113. {
  114. // Compute relative point and get the angle towards it
  115. var relative = transform.InverseTransformPoint(targetPos);
  116. var angle = Mathf.Atan2 (relative.x, relative.z) * Mathf.Rad2Deg;
  117. // Clamp it with the max rotation speed
  118. var maxRotation = rotateSpeed * Time.deltaTime;
  119. var clampedAngle = Mathf.Clamp(angle, -maxRotation, maxRotation);
  120. // Rotate
  121. transform.Rotate(0, clampedAngle, 0);
  122. // Return the current angle
  123. return angle;
  124. }
  125.  
  126. function Attack ()
  127. {
  128. isAttacking = true;
  129.  
  130. if (attackSound)
  131. {
  132. if (audio.clip != attackSound)
  133. {
  134. audio.Stop(); // stop the idling audio so we can switch out the audio clip.
  135. audio.clip = attackSound;
  136. audio.loop = true; // change the clip, then play
  137. audio.Play();
  138. }
  139. }
  140.  
  141. // Already queue up the attack run animation but set it's blend wieght to 0
  142. // it gets blended in later
  143. // it is looping so it will keep playing until we stop it.
  144. animation.Play("attackrun");
  145.  
  146. // First we wait for a bit so the player can prepare while we turn around
  147. // As we near an angle of 0, we will begin to move
  148. var angle : float;
  149. angle = 180.0;
  150. var time : float;
  151. time = 0.0;
  152. var direction : Vector3;
  153. while (angle > 5 || time < attackTurnTime)
  154. {
  155. time += Time.deltaTime;
  156. angle = Mathf.Abs(RotateTowardsPosition(target.position, rotateSpeed));
  157. move = Mathf.Clamp01((90 - angle) / 90);
  158.  
  159. // depending on the angle, start moving
  160. animation["attackrun"].weight = animation["attackrun"].speed = move;
  161. direction = transform.TransformDirection(Vector3.forward * attackSpeed * move);
  162. characterController.SimpleMove(direction);
  163.  
  164. yield;
  165. }
  166.  
  167. // Run towards player
  168. var timer = 0.0;
  169. var lostSight = false;
  170. while (timer < extraRunTime)
  171. {
  172. angle = RotateTowardsPosition(target.position, attackRotateSpeed);
  173.  
  174. // The angle of our forward direction and the player position is larger than 50 degrees
  175. // That means he is out of sight
  176. if (Mathf.Abs(angle) > 40)
  177. lostSight = true;
  178.  
  179. // If we lost sight then we keep running for some more time (extraRunTime).
  180. // then stop attacking
  181. if (lostSight)
  182. timer += Time.deltaTime;
  183.  
  184. // Just move forward at constant speed
  185. direction = transform.TransformDirection(Vector3.forward * attackSpeed);
  186. characterController.SimpleMove(direction);
  187.  
  188. // Keep looking if we are hitting our target
  189. // If we are, knock them out of the way dealing damage
  190. var pos = transform.TransformPoint(punchPosition);
  191. if(Time.time > lastPunchTime + 0.3 && (pos - target.position).magnitude < punchRadius)
  192. {
  193. // deal damage
  194. //target.SendMessage("ApplyDamage", damage); 5/19 commented this line - didn't work
  195. // knock the player back and to the side
  196. var slamDirection = transform.InverseTransformDirection(target.position - transform.position);
  197. slamDirection.y = 0;
  198. slamDirection.z = 1;
  199. if (slamDirection.x >= 0)
  200. slamDirection.x = 1;
  201. else
  202. slamDirection.x = -1;
  203. target.SendMessage("Slam", transform.TransformDirection(slamDirection));
  204. lastPunchTime = Time.time;
  205. }
  206.  
  207. // We are not actually moving forward.
  208. // This probably means we ran into a wall or something. Stop attacking the player.
  209. if (characterController.velocity.magnitude < attackSpeed * 0.3)
  210. break;
  211.  
  212. // yield for one frame
  213. yield;
  214. }
  215.  
  216. isAttacking = false;
  217.  
  218. // Now we can go back to playing the idle animation
  219. animation.CrossFade("idle");
  220. }
  221.  
  222. function ApplyDamage ()
  223. {
  224. animation.CrossFade("gothit");
  225. }
  226.  
  227. function OnDrawGizmosSelected ()
  228. {
  229. Gizmos.color = Color.yellow;
  230. Gizmos.DrawWireSphere (transform.TransformPoint(punchPosition), punchRadius);
  231. Gizmos.color = Color.red;
  232. Gizmos.DrawWireSphere (transform.position, attackDistance);
  233. }
  234.  
  235. @script RequireComponent(AudioSource)
Add Comment
Please, Sign In to add comment