Advertisement
draxdeveloper

Untitled

Apr 10th, 2014
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class EnemyShooter : MonoBehaviour {
  5.    
  6.     public GameObject bullet;
  7.     public float FireRate;
  8.     public float CollisionRadius;
  9.    
  10.     private float nextFire;
  11.    
  12.     // Use this for initialization
  13.     void Start () {
  14.        
  15.     }
  16.    
  17.     // Update is called once per frame
  18.     void Update () {
  19.        
  20.         if (CanFire())
  21.         {
  22.             Fire();
  23.         }
  24.     }
  25.    
  26.     private bool CanFire()
  27.     {
  28.         return (Time.time > nextFire) && (HasCollidedOnPlayer()) && (PlayerIsVisible());
  29.     }
  30.    
  31.     private bool HasCollidedOnPlayer()
  32.     {
  33.         Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, CollisionRadius);
  34.        
  35.         foreach (Collider2D collider in colliders)
  36.         {
  37.             if (collider.tag.Equals("Player"))
  38.             {
  39.                 return true;
  40.             }
  41.         }
  42.        
  43.         return false;
  44.     }
  45.    
  46.     private void Fire()
  47.     {
  48.         Instantiate(bullet, transform.position, transform.rotation);
  49.         nextFire = Time.time + FireRate;
  50.     }
  51.    
  52.     private bool PlayerIsVisible()
  53.     {
  54.         GameObject player = GameObject.FindGameObjectWithTag("Player");
  55.         return !player.GetComponent<InvisibilitySkill>().IsExecuting();
  56.     }
  57.    
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement