Unity 3D 泰课网打砖块实例 可以用来发射炮弹

说先看下运行之后的效果图
这里写图片描述
可以不断的从摄像机的位置发射出小球!去推倒墙体
这个写了三个脚本,都挂载在摄像机上面!
下载地址
首先看一下如何通过代码造出墙体出来
这里我们造出一个行列纵的一个墙体

using UnityEngine;
using System.Collections;

public class BrickBld : MonoBehaviour {

    public GameObject obj;//需要的砖块
    public int coluNumber=10;//列数
    public int rowNumber = 6;//行数
    public int zNumber = 2;//纵数
    // Use this for initialization
    void Start () {
        //Instantiate (obj);
        for (int zIndex=0; zIndex<zNumber; zIndex++) {
                        for (int rowIndex=0; rowIndex<rowNumber; rowIndex++) {
                                for (int coluIndex=0; coluIndex<coluNumber; coluIndex++) {
                                        Instantiate (obj, new Vector3 (coluIndex - 10, rowIndex + 0.5f, zIndex+5.0f), Quaternion.identity);
                                }
                        }
                }
    }

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

    }
}

最内层的循环造出一行,上一次造出一列,最外层造一纵,
下面看一下打击的代码和移动摄像机的代码

using UnityEngine;
using System.Collections;

public class Biu : MonoBehaviour {

    public GameObject BiuPosition;//位置点
    public float speed = 1000.0f;//s速度
    public Rigidbody bill;//物体
    // Use this for initialization
    public float speedMove = 100.0f;

    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButton (1)) {

                        Rigidbody rig = Instantiate (bill, BiuPosition.transform.position, Quaternion.identity) as Rigidbody;
                        rig.AddForce (speed * BiuPosition.transform.forward);

                }
        float h = Input.GetAxis ("Horizontal")*speedMove * Time.deltaTime;
        float v = Input.GetAxis ("Vertical") * speedMove * Time.deltaTime;

        transform.Translate (h,v,0.0f);
    }

}

给小球一个向前的力,将小球射出!
释放小球的代码

using UnityEngine;
using System.Collections;

public class Destory : MonoBehaviour {


    // Use this for initialization
    void Start () {
        //Destory (this.gameObject, 0.5f);
        Destroy (this.gameObject, 2.5f);
    }

    // Update is called once per frame
    void Update () {
//      Destory (this.gameObject, 0.5f);
    }
}

小球发射出之后两秒后释放!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值