Search Unity

Help With AI Stop Shooting Through Walls.

Discussion in 'Scripting' started by AusAndrew19, Jan 24, 2013.

  1. AusAndrew19

    AusAndrew19

    Joined:
    Jun 6, 2012
    Posts:
    16
    Hi Guys... My Unity3D Game Is Coming Along Beautifuly. 27 Weapons to chose 12 maps so far... Now im working on basic AI.. I have a script Based on TechZone FPS Tuts. but i need my enemy only to shoot me when he can see me.. I know i have to insert some type of raycast so it hits my tag "Player" it will attack... This is the script.

    Code (csharp):
    1. var speed = 3.0;
    2. var rotationSpeed = 5.0;
    3. var shootRange = 15.0;
    4. var attackRange = 30.0;
    5. var shootAngle = 4.0;
    6. var dontComeCloserRange = 5.0;
    7. var delayShootTime = 0.35;
    8. var pickNextWaypointDistance = 2.0;
    9. var target : Transform;
    10.  
    11. private var lastShot = -10.0;
    12.  
    13. // Make sure there is always a character controller
    14. @script RequireComponent (CharacterController)
    15.  
    16. function Start () {
    17.     // Auto setup player as target through tags
    18.     if (target == null  GameObject.FindWithTag("Player"))
    19.         target = GameObject.FindWithTag("Player").transform;
    20.  
    21.     Patrol();
    22. }
    23.  
    24. function Patrol () {
    25.     var curWayPoint = AutoWayPoint.FindClosest(transform.position);
    26.     while (true) {
    27.         var waypointPosition = curWayPoint.transform.position;
    28.         // Are we close to a waypoint? -> pick the next one!
    29.         if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance)
    30.             curWayPoint = PickNextWaypoint (curWayPoint);
    31.  
    32.         // Attack the player and wait until
    33.         // - player is killed
    34.         // - player is out of sight    
    35.         if (CanSeeTarget ())
    36.             yield StartCoroutine("AttackPlayer");
    37.        
    38.         // Move towards our target
    39.         MoveTowards(waypointPosition);
    40.        
    41.         yield;
    42.     }
    43. }
    44.  
    45.  
    46. function CanSeeTarget () : boolean {
    47.     if (Vector3.Distance(transform.position, target.position) > attackRange)
    48.         return false;
    49.        
    50.     var hit : RaycastHit;
    51.     if (Physics.Linecast (transform.position, target.position, hit))
    52.         return hit.transform == target;
    53.  
    54.     return false;
    55. }
    56.  
    57. function Shoot () {
    58.     // Start shoot animation
    59.     animation.CrossFade("shoot", 0.3);
    60.  
    61.     // Wait until half the animation has played
    62.     yield WaitForSeconds(delayShootTime);
    63.    
    64.     // Fire gun
    65.     BroadcastMessage("Fire");
    66.    
    67.     // Wait for the rest of the animation to finish
    68.     yield WaitForSeconds(animation["shoot"].length - delayShootTime);
    69. }
    70.  
    71. function AttackPlayer () {
    72.     var lastVisiblePlayerPosition = target.position;
    73.     while (true) {
    74.         if (CanSeeTarget ()) {
    75.             // Target is dead - stop hunting
    76.             if (target == null)
    77.                 return;
    78.  
    79.             // Target is too far away - give up
    80.             var distance = Vector3.Distance(transform.position, target.position);
    81.             if (distance > shootRange * 3)
    82.                 return;
    83.            
    84.             lastVisiblePlayerPosition = target.position;
    85.             if (distance > dontComeCloserRange)
    86.                 MoveTowards (lastVisiblePlayerPosition);
    87.             else
    88.                 RotateTowards(lastVisiblePlayerPosition);
    89.  
    90.             var forward = transform.TransformDirection(Vector3.forward);
    91.             var targetDirection = lastVisiblePlayerPosition - transform.position;
    92.             targetDirection.y = 0;
    93.  
    94.             var angle = Vector3.Angle(targetDirection, forward);
    95.  
    96.             // Start shooting if close and play is in sight
    97.             if (distance < shootRange  angle < shootAngle)
    98.                 yield StartCoroutine("Shoot");
    99.         } else {
    100.             yield StartCoroutine("SearchPlayer", lastVisiblePlayerPosition);
    101.             // Player not visible anymore - stop attacking
    102.             if (!CanSeeTarget ())
    103.                 return;
    104.         }
    105.  
    106.         yield;
    107.     }
    108. }
    109.  
    110. function SearchPlayer (position : Vector3) {
    111.     // Run towards the player but after 3 seconds timeout and go back to Patroling
    112.     var timeout = 3.0;
    113.     while (timeout > 0.0) {
    114.         MoveTowards(position);
    115.  
    116.         // We found the player
    117.         if (CanSeeTarget ())
    118.             return;
    119.  
    120.         timeout -= Time.deltaTime;
    121.         yield;
    122.     }
    123. }
    124.  
    125. function RotateTowards (position : Vector3) {
    126.     SendMessage("SetSpeed", 0.0);
    127.    
    128.     var direction = position - transform.position;
    129.     direction.y = 0;
    130.     if (direction.magnitude < 0.1)
    131.         return;
    132.    
    133.     // Rotate towards the target
    134.     transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
    135.     transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
    136. }
    137.  
    138. function MoveTowards (position : Vector3) {
    139.     var direction = position - transform.position;
    140.     direction.y = 0;
    141.     if (direction.magnitude < 0.5) {
    142.         SendMessage("SetSpeed", 0.0);
    143.         return;
    144.     }
    145.    
    146.     // Rotate towards the target
    147.     transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
    148.     transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
    149.  
    150.     // Modify speed so we slow down when we are not facing the target
    151.     var forward = transform.TransformDirection(Vector3.forward);
    152.     var speedModifier = Vector3.Dot(forward, direction.normalized);
    153.     speedModifier = Mathf.Clamp01(speedModifier);
    154.  
    155.     // Move the character
    156.     direction = forward * speed * speedModifier;
    157.     GetComponent (CharacterController).SimpleMove(direction);
    158.    
    159.     SendMessage("SetSpeed", speed * speedModifier, SendMessageOptions.DontRequireReceiver);
    160. }
    161.  
    162. function PickNextWaypoint (currentWaypoint : AutoWayPoint) {
    163.     // We want to find the waypoint where the character has to turn the least
    164.  
    165.     // The direction in which we are walking
    166.     var forward = transform.TransformDirection(Vector3.forward);
    167.  
    168.     // The closer two vectors, the larger the dot product will be.
    169.     var best = currentWaypoint;
    170.     var bestDot = -10.0;
    171.     for (var cur : AutoWayPoint in currentWaypoint.connected) {
    172.         var direction = Vector3.Normalize(cur.transform.position - transform.position);
    173.         var dot = Vector3.Dot(direction, forward);
    174.         if (dot > bestDot  cur != currentWaypoint) {
    175.             bestDot = dot;
    176.             best = cur;
    177.         }
    178.     }
    179.    
    180.     return best;
    181. }

    Any Help Will Be Appreciated :)