Advertisement
Guest User

Need Help Fixing Errors

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