Unity3D开发类似保龄球游戏

先学习一些基本的脚本实现:

1.动态创建物体.默认位置是(0,0)位置

GameObject goNew = GameObject.CreatePrimitive(PrimitiveType.Cube);
//创建的位置

goNew.transform.position = new Vector3(0, 0, -2);

 goNew.AddComponent<Rigidbody>();//添加刚体组件,是一种泛型


2.判断用户是否按下鼠标左键

if(Inut.GetMouseButtonDown(0))


3.按下鼠标左键,给它一个往前的脉冲力,forward就是一个默认长度为1的单位向量

this.gameObject.rigidbody.AddForce(Vector3.forward * 50, ForceMode.Impulse);


4.给当前物体添加一个往鼠标点击的方向的多大的力,它就会往那个方向去走

 //点击目标然后从摄像机的位置发射出一个小球,这里要计算力的方向向量

 Vector3 targetPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 3));
Vector3 dir = targetPos - Camera.main.transform.position;
//给当前的物体添加某个方向的力
this.gameObject.rigidbody.AddForce(dir * 5,ForceMode.Impulse);


5.点击鼠标生成对象
if (Input.GetMouseButtonDown(0))
{
     GameObject goNew = GameObject.CreatePrimitive(PrimitiveType.Sphere);
     goNew.transform.position = new Vector3(0, 0, 0);
      goNew.AddComponent<Rigidbody>();
}


6.对象销毁回收内存
if (Input.GetMouseButtonDown(0))
{
      GameObject s1 = GameObject.Find("Sphere");//相当于document.getElementById();
       Destroy(s1,2); //延时2秒销毁对象
}



制作游戏:

using UnityEngine;
using System.Collections;


public class gameText : MonoBehaviour {


    private GameObject goPlane;


// Use this for initialization
void Start () {
        //找到地形对象
        goPlane = GameObject.Find("Plane");


        //创建4*4的cube
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                go.transform.position = new Vector3(i, j, -1);
                go.AddComponent<Rigidbody>();
                go.AddComponent<AutoDistory>();//相当于实例化一个脚本销毁对象的一个类然后挂到每个对象中,让他不可见的时候自行销毁
            }
        }
}

// Update is called once per frame
void Update () {
        if (Input.GetMouseButtonDown(0))
        {
            //创建子弹的object
            GameObject goBullet = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            goBullet.transform.position = Camera.main.transform.position;
            goBullet.AddComponent<Rigidbody>();
            //让对象不可见的时候自行销毁
            goBullet.AddComponent<AutoDistory>();
            
            //获取到这个对象的多有资源,在发射的时候播放一个音乐
            goPlane.GetComponent<AudioSource>().Play();


            //点击鼠标,从摄像机的位置开始发射小球
            Vector3 targetPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 3));
            goBullet.rigidbody.AddForce((targetPos - Camera.main.transform.position) * 20, ForceMode.Impulse);
            
        }

}
    void OnGUI()
        {
            string s = "作者:丁小未";
            GUIStyle bb = new GUIStyle();
            bb.normal.background = null;//设置背景
            bb.normal.textColor = new Color(1,0,0);//设置颜色
            bb.fontSize = 40;
            GUI.Label(new Rect(40, 10, 100, 50), s, bb);
  
        }
}


AutoDistory脚本:

using UnityEngine;
using System.Collections;

//当东西不可见的时候就让他自动销毁
public class AutoDistory : MonoBehaviour {

// Use this for initialization
void Start () {
}

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


    void OnBecameInvisible()
    {
        Destroy(this.gameObject);
    }
}



其他提示:

1.天空盒的导入,提醒不要全部导入,不然文件会很大,应用是点击Edit-》Render Setting,然后导入天空盒

2.音频文件是在Camera上添加Component->Audio->Audio Sourse,他自动附带的Audio Listenner


详细项目源码:http://download.csdn.net/my



==================== 迂者 丁小未 CSDN博客专栏=================

MyBlog:http://blog.csdn.net/dingxiaowei2013             MyQQ:1213250243

Unity QQ群:858550         cocos2dx QQ群:280818155

====================== 相互学习,共同进步 ===================

 

转载请注明出处:http://blog.csdn.net/dingxiaowei2013/article/details/9734935

欢迎关注我的微博:http://weibo.com/u/2590571922


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值