CakeMeister

2d shooter UFO1 phan 7

Jul 31st, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 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.    
  13.     // Use this for initialization
  14.     void Start () {
  15.         player = GameObject.FindGameObjectWithTag("Player");
  16.        
  17.     }
  18.    
  19.     // Update is called once per frame
  20.     void Update () {
  21.         timer += Time.deltaTime;
  22.         shotdelay += Time.deltaTime;
  23.         if (health <= 0)
  24.             Destroy(this.gameObject);
  25.  
  26.         transform.Translate(direction * Time.deltaTime/0.4f);
  27.  
  28.         if (timer > 1)
  29.         {
  30.             timer = 0;
  31.             newposition();
  32.            
  33.         }
  34.  
  35.         if (shotdelay >= 3)
  36.         {
  37.             shotdelay = 0;
  38.             Vector3 playerdirection = player.transform.position - this.transform.position;
  39.             playerdirection.Normalize();
  40.             var rotationAngle = Quaternion.LookRotation(player.transform.position - transform.position);
  41.             rotationAngle.y = 0;
  42.        
  43.             GameObject bulletclone = Instantiate(bullet,this.transform.position, rotationAngle) as GameObject;
  44.             bulletclone.GetComponent<Rigidbody2D>().velocity = playerdirection * speed;
  45.  
  46.             Destroy(bulletclone,4f);
  47.  
  48.         }
  49.  
  50.     }
  51.  
  52.     void Damage(int dmg)
  53.     {
  54.         health -= dmg;
  55.     }
  56.     void newposition()
  57.     {
  58.         direction = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), 0);
  59.        
  60.     }
  61.  
  62.  
  63.    
  64.    
  65. }
Add Comment
Please, Sign In to add comment