Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Enemy Shooting through walls [Help]

Discussion in 'Scripting' started by wana7262, Sep 14, 2012.

  1. wana7262

    wana7262

    Joined:
    Jul 2, 2012
    Posts:
    103
    Hi all, i have managed to merge tow scripts now enemy in game can see player and react within his FOV
    But problem is that the enemy continue shooting and hurting the player even through a wall... here is the script can some one help me

    Code (csharp):
    1.  
    2. var target : Transform; //the enemy's target
    3. var moveSpeed = 3; //move speed
    4. var rotationSpeed = 3; //speed of turning
    5. var attackRange = 13; // distance within which to attack
    6. var chaseRange = 15; // distance within which to start chasing
    7. var giveUpRange = 18; // distance beyond which AI gives up
    8. var attackRepeatTime : float = 0.5; // delay between attacks when within range
    9. var anim : GameObject;
    10. var maximumHitPoints = 5.0;
    11. var hitPoints = 5.0;
    12. var attack : AudioClip;
    13. private var chasing = false;
    14. private var attackTime : float;
    15. var checkRay : boolean = false;
    16. var idleAnim : String = "idle";
    17. var walkAnim : String = "walk";
    18. var attackAnim : String = "attack";
    19. var wanderAnim: String = "wander";
    20. var dontComeCloserRange : int = 4;
    21. var ZmaximumHitPoints = 100.0;
    22. var ZhitPoints = 100.0;
    23. var deadReplacement : Rigidbody;
    24. var GOPos : GameObject;
    25. private var scoreManager : ScoreManager;
    26.  
    27. var playerObject : GameObject; // the player
    28. var fieldOfViewRange : float; // in degrees (I use 68, this gives the enemy a vision of 136 degrees)
    29. var minPlayerDetectDistance : float; // the distance the player can come behind the enemy without being deteacted
    30. var rayRange : float; // distance the enemy can "see" in front of him
    31. private var rayDirection = Vector3.zero;
    32.  
    33.  
    34. private var myTransform : Transform; //current transform data of this enemy
    35.  
    36. function Awake(){
    37.     myTransform = transform; //cache transform data for easy access/preformance
    38.     anim.animation.wrapMode = WrapMode.Loop;
    39.     anim.animation[attackAnim].wrapMode = WrapMode.Once;
    40.     anim.animation[attackAnim].layer = 2;
    41.     anim.animation.Stop();
    42. }
    43.  
    44. function Start(){
    45.     target = GameObject.FindWithTag("Player").transform;
    46.     var GO = gameObject.FindWithTag("ScoreManager");
    47.     scoreManager = GO.GetComponent("ScoreManager");
    48. }
    49. function ApplyDamage (damage : float) {
    50.     if (ZhitPoints <= 0.0)
    51.         return;
    52.     // Apply damage
    53.     ZhitPoints -= damage;
    54.     scoreManager.DrawCrosshair();
    55.     // Are we dead?
    56.     if (ZhitPoints <= 0.0)
    57.         Replace();
    58. }
    59.  
    60. function Replace() {
    61.  
    62.     // If we have a dead barrel then replace ourselves with it!
    63.     if (deadReplacement) {
    64.         var dead : Rigidbody = Instantiate(deadReplacement, GOPos.transform.position, GOPos.transform.rotation);
    65.         scoreManager.addScore(20);
    66.         // For better effect we assign the same velocity to the exploded barrel
    67.         dead.rigidbody.velocity = rigidbody.velocity;
    68.         dead.angularVelocity = rigidbody.angularVelocity;
    69.     }
    70.     // Destroy ourselves
    71.     Destroy(gameObject);
    72. }
    73.  
    74. function Update () {
    75.     // check distance to target every frame:
    76.     var distance = (target.position - myTransform.position).magnitude;
    77.  
    78.         if (distance < dontComeCloserRange)
    79.         {
    80.             moveSpeed = 0;
    81.            
    82.             anim.animation[idleAnim].speed = 0.2;
    83.             anim.animation.CrossFade(idleAnim);
    84.         }else
    85.         {
    86.             moveSpeed = Random.Range(4, 6);
    87.             anim.animation.CrossFade(walkAnim);
    88.         }
    89.        
    90.     if (chasing || ZhitPoints  < 100)
    91.     {  
    92.         GetComponent("WanderBehavior").enabled = !GetComponent("WanderBehavior").enabled;
    93.         //move towards the player
    94.         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
    95.        
    96.  
    97.         //rotate to look at the player
    98.         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    99.         transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
    100.  
    101.         // give up, if too far away from target:
    102.         if (distance > giveUpRange)
    103.         {
    104.             chasing = false;
    105.             GetComponent("WanderBehavior").enabled = true;
    106.         }
    107.  
    108.                         // attack, if close enough, and if time is OK:
    109.         if (distance < attackRange  Time.time > attackTime)
    110.         {
    111.                             var hit : RaycastHit;
    112.                             rayDirection = playerObject.transform.position - transform.position;
    113.                             if((Vector3.Angle(rayDirection, transform.forward)) < fieldOfViewRange)
    114.                             { // Detect if player is within the field of view
    115.                             if (Physics.Raycast (transform.position, rayDirection, hit, rayRange))
    116.                             {
    117.                
    118.                             if (hit.transform.tag == "Player")
    119.                             {
    120.                                
    121.                                 target.SendMessage( "PlayerDamage", maximumHitPoints);
    122.                                
    123.                             }
    124.                             else
    125.                             {
    126.                                 //Debug.Log("Can not see player");
    127.                                 chasing =  false;
    128.                             }
    129.                             }
    130.                             }
    131.             anim.animation[attackAnim].speed = 1.2;
    132.             //anim.animation.CrossFade(attackAnim);
    133.             anim.animation.Play(attackAnim);
    134.             attackTime = Time.time + attackRepeatTime;
    135.             audio.PlayOneShot(attack, 0.5 / audio.volume);
    136.  
    137.         }
    138.  
    139.     }
    140.      else
    141.      {
    142.         // not currently chasing.
    143.             anim.animation[wanderAnim].speed = 1.2;
    144.             anim.animation.Play(wanderAnim);
    145.  
    146.         // start chasing if target comes close enough
    147.         if (CanSeeTarget())//(distance < chaseRange)
    148.          {
    149.             chasing = true;
    150.         }
    151.         else
    152.          chasing = false;
    153.     }
    154. }
    155.  
    156. function OnDrawGizmosSelected ()
    157. {
    158. // Draws a line in front of the player and one behind this is used to visually illustrate the detection ranges in front and behind the enemy
    159.     Gizmos.color = Color.magenta; // the color used to detect the player in front
    160.     Gizmos.DrawRay (transform.position, transform.forward * rayRange);
    161.     Gizmos.color = Color.yellow; // the color used to detect the player from behind
    162.     Gizmos.DrawRay (transform.position, transform.forward * -minPlayerDetectDistance);    
    163. }
    164.  
    165. function CanSeeTarget () : boolean
    166. {
    167.     var hit : RaycastHit;
    168.     rayDirection = playerObject.transform.position - transform.position;
    169.     var distanceToPlayer = Vector3.Distance(transform.position, playerObject.transform.position);
    170.  
    171.     if(Physics.Raycast (transform.position, rayDirection, hit))
    172.     { // If the player is very close behind the enemy and not in view the enemy will detect the player
    173.         if((hit.transform.tag == "Player")  (distanceToPlayer <= minPlayerDetectDistance)){
    174.             //Debug.Log("Caught player sneaking up behind!");
    175.             return true;
    176.         }
    177.     }
    178.  
    179.  
    180.     if((Vector3.Angle(rayDirection, transform.forward)) < fieldOfViewRange)
    181.     { // Detect if player is within the field of view
    182.         if (Physics.Raycast (transform.position, rayDirection, hit, rayRange))
    183.         {
    184.  
    185.             if (hit.transform.tag == "Player") {
    186.                 //Debug.Log("Can see player");
    187.                 return true;
    188.             }
    189.             else
    190.             {
    191.                 //Debug.Log("Can not see player");
    192.                 return false;
    193.             }
    194.         }
    195.     }
    196. }
    197.  
    198.  
    199.  
     
  2. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    does the npc shoot a raycast at the player? Could the wall be on a different layer such as ignore raycast?