Advertisement
Guest User

Stuff

a guest
May 24th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. #pragma strict
  2.  
  3. var target : Transform; //the enemy's target
  4.  
  5. var moveSpeed = 3; //move speed
  6.  
  7. var rotationSpeed = 3; //speed of turning
  8.  
  9. var attackThreshold = 1.5; // distance within which to attack
  10.  
  11. var chaseThreshold = 10; // distance within which to start chasing
  12.  
  13. var giveUpThreshold = 20; // distance beyond which AI gives up
  14.  
  15. var attackRepeatTime = 1; // delay between attacks when within range
  16.  
  17. private var chasing = false;
  18.  
  19. public var attackTime : float;
  20.  
  21. var myTransform : Transform; //current transform data of this enemy
  22.  
  23. function Awake()
  24.  
  25. {
  26.  
  27. myTransform = transform; //cache transform data for easy access/preformance
  28.  
  29. }
  30.  
  31.  
  32.  
  33.  
  34.  
  35. function Start()
  36.  
  37. {
  38.  
  39. if (target == null && GameObject.FindWithTag("Player"))
  40.  
  41. target = GameObject.FindWithTag("Player").transform;
  42.  
  43. }
  44.  
  45. function Update () {
  46.  
  47. // check distance to target every frame:
  48.  
  49. var distance = (target.position - myTransform.position).magnitude;
  50.  
  51.  
  52.  
  53. if (chasing) {
  54.  
  55.  
  56.  
  57. //rotate to look at the player
  58.  
  59. myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
  60.  
  61. Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
  62.  
  63.  
  64.  
  65. //move towards the player
  66.  
  67. myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
  68.  
  69.  
  70.  
  71. animation.Play("ZombieWalk");
  72.  
  73.  
  74.  
  75. // give up, if too far away from target:
  76.  
  77. if (distance > giveUpThreshold) {
  78.  
  79. chasing = false;
  80.  
  81. }
  82.  
  83.  
  84.  
  85. // attack, if close enough, and if time is OK:
  86.  
  87. if (distance < attackThreshold && Time.time > attackRepeatTime) {
  88.  
  89. animation.Play("ZombieAttack");
  90.  
  91. target.SendMessage("ApplyDamage",10);
  92.  
  93. // Attack! (call whatever attack function you like here)
  94.  
  95. }
  96.  
  97. if (distance < attackThreshold) {
  98.  
  99. moveSpeed=0;
  100.  
  101. //stop when close enough
  102.  
  103. }
  104.  
  105.  
  106.  
  107. attackTime = Time.time+ attackRepeatTime;
  108.  
  109. }
  110.  
  111.  
  112.  
  113. else {
  114.  
  115. // not currently chasing.
  116.  
  117. animation.Play("ZombieIdle");
  118.  
  119. // start chasing if target comes close enough
  120.  
  121. if (distance < chaseThreshold) {
  122.  
  123. chasing = true;
  124.  
  125. }
  126.  
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement