- 排名
- 1
- 昨日变化

- UID
- 2
- 好友
- 123
- 蛮牛币
- 4279
- 威望
- 121
- 注册时间
- 2013-5-19
- 在线时间
- 2359 小时
- 最后登录
- 2019-10-28

|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?注册帐号
x
本帖最后由 admin 于 2013-6-7 09:59 编辑
在前几天的总结中,已经给篮球加过钢体 碰撞体 乃至于材质,那么我们又将如何检测碰撞呢?如何计算投中篮球的分数呢?
1.碰撞检测
首先我们先创建一个空对象
然后给这个空对象加上一个碰撞体
最后在层次窗体中选择这个空对象,在属性窗体中设置好它的大小、位置
下面将以代码的方式来实现碰撞检测- //检测碰撞
- void OnTriggerEnter(Collider other)
- {
- //获取名字
- if (other.gameObject.name=="basketballPrefab(Clone)")
- {
- //分数加2
- score +=2;
- }
- }
复制代码 注:OnTriggerEnter 表示碰撞检测的方法- //绘制界面、
- void OnGUI()
- {
- //绘制一个label,显示倒计时
- GUI.Label (new Rect (10,10,100,20),"Time left:"+timeLeft.ToString () );
- //绘制一个label,显示分数
- GUI.Label (new Rect (10,30,1000,20),"Score:"+score);
- if (showEndbutton )
- {
- //点击Restart Game按钮,则重置游戏
- if (GUI.Button (new Rect (Screen.width/2-50,Screen.height/2-50,100,40),"Restart Game"))
- Restart();
- //如果点击Publish Score按钮,则跳转到分数面板
- if (GUI.Button (new Rect (Screen.width/2-50,Screen.height/2,100,40),"Publish Score"))
- Application.LoadLevel ("Score");
- //如果点击Exit Game,则退出游戏
- if (GUI.Button (new Rect (Screen.width/2-50,Screen.height/2+50,100,40),"Exit Game"))
- Application.Quit ();
- }
- }
- //重置游戏场景
- public void Restart()
- {
- showEndbutton =false ;
- player.isPlay =true ;
-
- isPlay =true;
-
- totalTime =0;
- timeLeft =timeLength ;
- score =0;
- }
-
- void Update ()
- {
- if (isPlay )
- {
- totalTime +=Time.deltaTime ;
- //判断当前时间是否大于0,大于0 获得剩余时间
- if (timeLeft >0)
- timeLeft =(int)(timeLength -totalTime) ;
- else if (timeLeft ==0)
- {
- //设置游戏暂停
- isPlay =false ;
- player.isPlay =false ;
- //运行子线程
- StartCoroutine (delayforSometime (5.0f));
- }
- }
- }
- IEnumerator delayforSometime(float time)
- {
- yield return new WaitForSeconds (time );
- //设置显示按钮
- showEndbutton =true ;
- }
复制代码 ok,通过上述设置及代码,运行游戏
1.游戏界面左上角显示剩余时间 和篮球进球的分数
2.当剩余时间为0 ,暂停游戏 弹出 菜单按钮 分别为 重置游戏 对比分数 退出游戏
怎么样?是不是开始有游戏的效果了呢?呵呵...继续加油
来自QQ群:290248177 By. ζޓއއއ 随风去旅行 QQ:512241701
|
|