简单制作《坦克大战》

控制坦克的移动

public class moveRen : MonoBehaviour {
    public float moveSpeed = 3f;
    public float roateSpeed = 9f;
    private Rigidbody target;
    void Start () {
        target = this.GetComponent<Rigidbody> ();
    }
    void FixedUpdate () {
        float v = Input.GetAxis ("Vertical");
        target.velocity = transform.right * v * moveSpeed;
        float h = Input.GetAxis ("Horizontal");
        target.angularVelocity = transform.up * h * roateSpeed;
    }
} 

发射子弹和自动发射子弹

public class TankShell : MonoBehaviour {
    public GameObject shell;
    public Transform shellPosition;
    public float shellSpeed = 10;
    private int count = 0;
    public bool isAuto = false;
    void shoot()
    {
        GameObject go = GameObject.Instantiate (shell, shellPosition.position, shellPosition.rotation) as GameObject;
        Rigidbody r = go.GetComponent<Rigidbody> ();
        r.velocity = shellPosition.forward * shellSpeed;
    }
    void FixedUpdate () {
        if (isAuto) {
            count++;
            if (count > 100) {
                count = 0;
                shoot ();
            }
        } else {
            if (Input.GetKeyDown (KeyCode.Space)) {
                shoot ();
            }
        }
    }
}

给子弹添加爆炸力

public class PengZhuang : MonoBehaviour {
    public GameObject explosion;
//    void OnCollisionEnter(Collision other)
//    {
//        //销毁脚本附加的物体
//        Destroy (gameObject);
//        GameObject obj = GameObject.Instantiate (explosion,transform.position,transform.rotation);
//        Destroy (obj,1f);
//    }

    void OnTriggerEnter(Collider other)
    {
        //销毁脚本附加的物体
        Destroy (gameObject);
        GameObject obj = GameObject.Instantiate (explosion,transform.position,transform.rotation);
        Destroy (obj,1f);
        if (other.tag == "Tank") {
            other.SendMessage ("TakeDamage");
        }
    } 

给坦克添加血量

public class XueLiang : MonoBehaviour {
    public float health = 100;
//    public void TakeDamage(float value)
//    {
//        if (health > value) {
//            health -= value;
//        } else {
//            Destroy (gameObject);
//        }
//    }

    public void TakeDamage()
    {
        if (health > 0) {
            health -= 100;
        }
        if (health <= 0) {
            GameObject.Destroy (this.gameObject);
        }
    }
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值