Advertisement
CakeMeister

Turret AI phan 12

Jul 6th, 2017
1,620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TurretAI : MonoBehaviour {
  6.  
  7.     public int curHealth = 100;
  8.  
  9.     public float distance;
  10.     public float wakerange;
  11.     public float shootinterval;
  12.     public float bulletspeed = 5;
  13.     public float bullettimer;
  14.  
  15.     public bool awake = false;
  16.     public bool lookingRight = true;
  17.  
  18.     public GameObject bullet;
  19.     public Transform target;
  20.     public Animator anim;
  21.     public Transform shootpointL, shootpointR;
  22.  
  23.  
  24.     private void Awake()
  25.     {
  26.         anim = GetComponent<Animator>();
  27.        
  28.     }
  29.     // Use this for initialization
  30.     void Start () {
  31.            
  32.     }
  33.    
  34.     // Update is called once per frame
  35.     void Update () {
  36.         anim.SetBool("Awake", awake);
  37.         anim.SetBool("LookRight", lookingRight);
  38.  
  39.         RangeCheck();
  40.  
  41.         if (target.transform.position.x > transform.position.x)
  42.         {
  43.             lookingRight = true;
  44.         }
  45.  
  46.         if (target.transform.position.x < transform.position.x)
  47.         {
  48.             lookingRight = false;
  49.         }
  50.  
  51.         if (curHealth < 0)
  52.         {
  53.             Destroy(gameObject);
  54.         }
  55.     }
  56.  
  57.  
  58.     void RangeCheck()
  59.     {
  60.         distance = Vector2.Distance(transform.position, target.transform.position);
  61.  
  62.         if (distance < wakerange)
  63.             awake = true;
  64.  
  65.         if (distance > wakerange)
  66.             awake = false;
  67.     }
  68.  
  69.     public void Attack(bool attackright)
  70.     {
  71.         bullettimer += Time.deltaTime;
  72.  
  73.         if (bullettimer >= shootinterval)
  74.         {
  75.             Vector2 direction = target.transform.position - transform.position;
  76.             direction.Normalize();
  77.  
  78.             if (attackright)
  79.             {
  80.                 GameObject bulletclone;
  81.                 bulletclone = Instantiate(bullet, shootpointR.transform.position, shootpointR.transform.rotation) as GameObject;
  82.                 bulletclone.GetComponent<Rigidbody2D>().velocity = direction * bulletspeed;
  83.  
  84.                 bullettimer = 0;
  85.             }
  86.  
  87.             if (!attackright)
  88.             {
  89.                 GameObject bulletclone;
  90.                 bulletclone = Instantiate(bullet, shootpointL.transform.position, shootpointL.transform.rotation) as GameObject;
  91.                 bulletclone.GetComponent<Rigidbody2D>().velocity = direction * bulletspeed;
  92.  
  93.                 bullettimer = 0;
  94.             }
  95.         }
  96.     }
  97.  
  98.     public void Damage(int dmg)
  99.     {
  100.         curHealth -= dmg;
  101.         gameObject.GetComponent<Animation>().Play("redflash");
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement