小游戏--旋转的小球
来源:互联网 发布:淘宝提高销量 编辑:程序博客网 时间:2024/05/29 15:57
这是个小游戏,最早出现在某个页游平台,玩法简单,先上图:
游戏主要分为3各部分, 1.小球旋转(这个太简单了,只有几句代码); 2. 生成并发射 针(我反正觉得像针); 3.检测发射结果(加分或者失败)
基本上没有什么难点,主要是检测碰撞的针头一定要绑碰撞器和刚体,用2D的就行了, 刚体记得要把重力设为0,不然针头会往下掉;
针头的tag可以新建一个 "Head",方便识别,或者就用已有的也行
代码有2个脚本,HeadCheck是绑在针头上的,另外一个放在最上层节点即可;所有Public对象是手动绑定的
using UnityEngine;using System.Collections;public class HeadCheck : MonoBehaviour { public EveryBitOfTime _GameRoot; //碰撞检测,需要绑定碰撞器(Collider2D)和 rigidbody2D private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "Head") { Debug.Log("GG"); _GameRoot.IsGameOver = true; } }}
using UnityEngine;using System.Collections;using UnityEngine.UI;public class EveryBitOfTime : MonoBehaviour { public Transform _circle;//转动的圆圈 public Text _txtScore;//得分 public Transform _needle_Prefab, _root;//针预制体 public GameObject _text_GG, _btn_Restart; Transform _needle_Fly, _needle_Start; float _speed = -30, _lv;//转动速度, 难度 bool _isShoot = false, _isFly = false, _isGameOver = false;//已经发射, 正在飞行中 int _score = 0; Vector3 _end; public bool IsGameOver { get { return _isGameOver; } set { _isGameOver = value; } } // Use this for initializationvoid Start () { _end = _circle.position; _end.y -= 0.55f;//去掉半径\ _text_GG.SetActive(false); _btn_Restart.SetActive(false);}// Update is called once per framevoid Update () { //每10个会加快转动 _lv = _score / 10; //旋转小圈 if (!_isGameOver) _circle.Rotate(new Vector3(0, 0, _speed * Time.deltaTime - _lv * 0.2f)); ShootNeedle();} /// <summary> /// 创建一个针在底部 /// </summary> /// <returns></returns> Transform CreateNeedle() { GameObject go = GameObject.Instantiate(_needle_Prefab.gameObject) as GameObject; go.SetActive(true); go.transform.SetParent(_root); go.transform.localPosition = new Vector3(0, -340, 0); go.transform.localScale = Vector3.one; return go.transform; } /// <summary> /// 发射 /// </summary> void ShootNeedle() { if (!_isShoot && Input.GetMouseButtonDown(0)) { _needle_Start = CreateNeedle(); _isShoot = true; _isFly = true; } if (_isFly) { //移动 _needle_Fly = _needle_Start; _needle_Fly.position = Vector3.MoveTowards(_needle_Fly.position, _end, 5.0f * Time.deltaTime); //到达终点 if (Vector3.Distance(_needle_Fly.position, _end) < 0.05f) { _needle_Fly.SetParent(_circle);//更新父节点,让针一起旋转 if (_isGameOver) { StartCoroutine(GameOver());//防止有时卡顿 Debug.Log("调用GameOver"); } else { _score++;//加分 _txtScore.text = _score.ToString(); _isShoot = false; _isFly = false; } } } } IEnumerator GameOver() { _text_GG.SetActive(true); _btn_Restart.SetActive(true); _isShoot = true; _isFly = false; yield return 0; } //按钮事件 public void BtnEvent_Restart() { _text_GG.SetActive(false); _btn_Restart.SetActive(false); _score = 0; _txtScore.text = "0"; _isGameOver = false; _isShoot = false; for (int i = 0; i < _circle.childCount; ++i) { Destroy(_circle.GetChild(i).gameObject); } }}
阅读全文
1 0
- 小游戏--旋转的小球
- aa小游戏,旋转的小球
- h5 canvas绘制的旋转小球
- LayaAir 自旋转的小球 横向移动
- android 小球绕外圆旋转
- Android-两个小球不停的绕中心旋转的进度条
- Android-两个小球不停的绕中心旋转的进度条
- OPengl实现小球围绕大球旋转的效果
- 自定义控件-绕着圆形轨迹旋转的小球
- 【JavaScript】实现三个小球围着椭圆旋转的加载动画
- LayaAir 自旋转的小球跟随鼠标移动
- cocos2d-x 3.2 物理小游戏教程3 block it 小球的运动 墙壁的隐藏
- iOS传感器:使用陀螺仪完成一个小球撞壁的小游戏
- iOS传感器:使用陀螺仪完成一个小球撞壁的小游戏
- IOS版本-两个小球不停的绕中心旋转的进度条
- Android动画之旅(二)----两个小球旋转的动画,形成视觉差的效果
- Flash AS3 学习8 - 旋转的小球
- html5 canvas实现小球绕圆旋转
- git 与github
- NOIP,我们又来了
- Error:Execution failed for task ':app:mergeDebugResources
- Android中实现自动轮播的框架(Banner)的介绍以及使用
- SQL批处理 导入excel数据到表
- 小游戏--旋转的小球
- socket阻塞与非阻塞,同步与异步、I/O模型
- C#.NET常见问题(FAQ)-override覆盖和virtual虚类如何理解
- C#.NET常见问题(FAQ)-构造器constructor有什么用
- java中equals用法以及==的用法(面试坑)
- C#.NET常见问题(FAQ)-public private protectd internal有什么区别
- angularjs购物车
- C#.NET常见问题(FAQ)-索引器indexer有什么用
- C#.NET常见问题(FAQ)-命名空间namespace如何理解