小游戏--旋转的小球

来源:互联网 发布:淘宝提高销量 编辑:程序博客网 时间: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