1. 程式人生 > >Unity坦克大戰敵方坦克受到傷害銷燬指令碼

Unity坦克大戰敵方坦克受到傷害銷燬指令碼

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class EnemyScript : MonoBehaviour {
    Transform player;
    float timer = 0;
    public GameObject bulletPrefab;
    public Transform FirePoint;
    public int hp = 3;
// Use this for initialization
void Start () {
        player = GameObject.FindWithTag("Player").transform;
}

// Update is called once per frame
void Update () {
        transform.LookAt(player);
        if (Vector3.Distance(transform.position, player.position) >= 10)
        {
            transform.position = Vector3.MoveTowards(transform.position,player.position,Time.deltaTime);
        }
        else
        {
            timer += Time.deltaTime;
            if (timer >= 1)
            {
                timer = 0;
                Debug.Log("t");
                GameObject bullet = Instantiate(bulletPrefab, FirePoint.position, transform.rotation) as GameObject;
            }
        }
        if (hp <= 0)
        {
            Destroy(this.gameObject);
        }
}
}