Advertisement
CakeMeister

UFO1 script

Aug 14th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ufo1 : MonoBehaviour {
  6.     public int health = 100;
  7.     public float timer = 1,shotdelay = 0,speed = 4f;
  8.     public Vector3 direction;
  9.  
  10.     public GameObject player;
  11.     public GameObject bullet;
  12.     public GameObject deatheffect;
  13.  
  14.     // Use this for initialization
  15.     void Start () {
  16.         player = GameObject.FindGameObjectWithTag("Player");
  17.  
  18.     }
  19.  
  20.     // Update is called once per frame
  21.     void Update () {
  22.         timer += Time.deltaTime;
  23.         shotdelay += Time.deltaTime;
  24.  
  25.         if (health <= 0)
  26.         {
  27.             Destroy(this.gameObject);
  28.             Destroy(Instantiate(deatheffect, this.transform.position, this.transform.rotation) as GameObject,2);
  29.  
  30.         }
  31.  
  32.         transform.Translate(direction * Time.deltaTime/0.4f);
  33.  
  34.         if (timer > 1)
  35.         {
  36.             timer = 0;
  37.             newposition();
  38.  
  39.         }
  40.  
  41.         if (shotdelay >= 3)
  42.         {
  43.             shotdelay = 0;
  44.             Vector3 playerdirection = player.transform.position - transform.position;
  45.             playerdirection.Normalize();
  46.             var rotation = Quaternion.LookRotation(player.transform.position - transform.position);
  47.             rotation.y = 0;
  48.             rotation.x = 0;
  49.  
  50.             GameObject bulletclone = Instantiate(bullet, transform.position, rotation) as GameObject;
  51.             bulletclone.GetComponent<Rigidbody2D>().velocity = playerdirection * speed;
  52.             Destroy(bulletclone, 4f);
  53.         }
  54.     }
  55.  
  56.     void Damage(int dmg)
  57.     {
  58.         health -= dmg;
  59.  
  60.     }
  61.  
  62.     void newposition()
  63.     {
  64.         direction = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), 0);
  65.  
  66.     }
  67.  
  68.  
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement