CakeMeister

2d shooter ufo2 phan 6

Jul 29th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ufo2 : MonoBehaviour
  6. {
  7.     public int health = 100;
  8.     public float speed = 100f;
  9.     public Vector3 direction;
  10.     public GameObject player;
  11.     public Collider2D col;
  12.     public Rigidbody2D r2;
  13.     // Use this for initialization
  14.     void Start()
  15.     {
  16.         player = GameObject.FindGameObjectWithTag("Player");
  17.         col = gameObject.GetComponentInChildren<Collider2D>();
  18.         r2 = GetComponent<Rigidbody2D>();
  19.     }
  20.  
  21.     // Update is called once per frame
  22.     void Update()
  23.     {
  24.  
  25.         if (health <= 0)
  26.             Destroy(this.gameObject);
  27.  
  28.         direction = player.transform.position - this.transform.position;
  29.         direction.Normalize();
  30.         r2.velocity = direction * speed;
  31.         Debug.Log("position: " + direction);
  32.  
  33.  
  34.     }
  35.  
  36.     private void OnCollisionEnter2D(Collision2D col)
  37.     {
  38.         if (col.collider.CompareTag("Player"))
  39.             transform.position = transform.position - (direction * 2);
  40.        else
  41.             transform.position = transform.position + (direction);
  42.  
  43.  
  44.     }
  45.  
  46.     void Damage(int dmg)
  47.     {
  48.         health -= dmg;
  49.     }
  50.  
  51.    
  52.  
  53. }
Add Comment
Please, Sign In to add comment