using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
///


/// HP,HP受损方法
///

public class TankHealth : MonoBehaviour
{
public int hp = 100;
public GameObject tankExplosion;
public AudioClip tankExplositionAudio;
public Slider hpSlider;
private int hpTotal;
// Use this for initialization
void Start()
{
hpTotal = hp;
}

// Update is called once per frame
void Update()
{

}
void TankDamage()
{
    if (hp <= 0) return;
    hp -= Random.Range(10, 20);
    hpSlider.value = (float)hp / hpTotal;
    if (hp <= 0)
    {
        AudioSource.PlayClipAtPoint(tankExplositionAudio, transform.position);
        GameObject.Instantiate(tankExplosion, transform.position + Vector3.up, transform.rotation);
        GameObject.Destroy(this.gameObject);
    }
}

}

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


/// 按下攻击,发射子弹向FirePoint前方飞进
///

public class TankAttack : MonoBehaviour
{

public GameObject shellPrefab;
public KeyCode fireKey = KeyCode.Space;
private Transform firePosition;
public float shellSpeed = 10;
public AudioClip shoutAudio;
// Use this for initialization
void Start()
{
    firePosition = transform.Find("FirePosition");
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(fireKey))
    {
        AudioSource.PlayClipAtPoint(shoutAudio, transform.position);
        GameObject go = GameObject.Instantiate(shellPrefab, firePosition.position, firePosition.rotation)as GameObject;
        go.GetComponent<Rigidbody>().velocity = go.transform.forward * shellSpeed;
    }
}

}

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


/// 子弹出发检测,(两物体都有触发器,其中一个有刚体组件,其中一个触发器勾选Is Tirgger)
///打中目标,调用目标受损方法
///

public class Shell : MonoBehaviour
{

public GameObject shellExplosionPrefab;
public  AudioClip shellExplosionAudio;


public void OnTriggerEnter(Collider collider)
{
    AudioSource.PlayClipAtPoint(shellExplosionAudio,transform.position);
    GameObject.Instantiate(shellExplosionPrefab, transform.position, transform.rotation);
    GameObject.Destroy(this.gameObject);
    if (collider.tag == "Tank")
    {
        collider.SendMessage("TankDamage");
    }

}

}