CakeMeister

TurretAI phan 16

Jul 13th, 2017
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 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.     public SoundManager sound;
  23.  
  24.  
  25.     private void Awake()
  26.     {
  27.         anim = GetComponent<Animator>();
  28.        
  29.     }
  30.     // Use this for initialization
  31.     void Start () {
  32.         sound = GameObject.FindGameObjectWithTag("sound").GetComponent<SoundManager>();
  33.     }
  34.    
  35.     // Update is called once per frame
  36.     void Update () {
  37.         anim.SetBool("Awake", awake);
  38.         anim.SetBool("LookRight", lookingRight);
  39.  
  40.         RangeCheck();
  41.  
  42.         if (target.transform.position.x > transform.position.x)
  43.         {
  44.             lookingRight = true;
  45.         }
  46.  
  47.         if (target.transform.position.x < transform.position.x)
  48.         {
  49.             lookingRight = false;
  50.         }
  51.  
  52.         if (curHealth < 0)
  53.         {
  54.             sound.Playsound("destroy");
  55.             Destroy(gameObject);
  56.         }
  57.     }
  58.  
  59.  
  60.     void RangeCheck()
  61.     {
  62.         distance = Vector2.Distance(transform.position, target.transform.position);
  63.  
  64.         if (distance < wakerange)
  65.             awake = true;
  66.  
  67.         if (distance > wakerange)
  68.             awake = false;
  69.     }
  70.  
  71.     public void Attack(bool attackright)
  72.     {
  73.         bullettimer += Time.deltaTime;
  74.  
  75.         if (bullettimer >= shootinterval)
  76.         {
  77.             Vector2 direction = target.transform.position - transform.position;
  78.             direction.Normalize();
  79.  
  80.             if (attackright)
  81.             {
  82.                 GameObject bulletclone;
  83.                 bulletclone = Instantiate(bullet, shootpointR.transform.position, shootpointR.transform.rotation) as GameObject;
  84.                 bulletclone.GetComponent<Rigidbody2D>().velocity = direction * bulletspeed;
  85.  
  86.                 bullettimer = 0;
  87.             }
  88.  
  89.             if (!attackright)
  90.             {
  91.                 GameObject bulletclone;
  92.                 bulletclone = Instantiate(bullet, shootpointL.transform.position, shootpointL.transform.rotation) as GameObject;
  93.                 bulletclone.GetComponent<Rigidbody2D>().velocity = direction * bulletspeed;
  94.  
  95.                 bullettimer = 0;
  96.             }
  97.         }
  98.     }
  99.  
  100.     public void Damage(int dmg)
  101.     {
  102.         curHealth -= dmg;
  103.         gameObject.GetComponent<Animation>().Play("redflash");
  104.     }
  105. }
Add Comment
Please, Sign In to add comment