像素鸟柱子的设置_03

来源:互联网 发布:淘宝网购物女装晚礼服 编辑:程序博客网 时间:2024/04/30 15:40

http://download.csdn.net/detail/yy763496668/9838697 源码下载地址
柱子的预制体制作的方法
这里写图片描述

拖拽一个精灵到场景中,Ctrl+D复制一份并将第二份z轴旋转180°,对两份同时添加Boxcollider 2D
并调整宽度。
创建一个空游戏对象作为以上两个游对象的父物体,更名为Columns,添Rigidbody2D,BoxCollider2D组件。并进行相关的设置,如图:
这里写图片描述
这里的碰撞器一定勾选IsTrigger,这里是用来检测小鸟有没有通过柱子,如果通过柱子,为以后加分准备。
像素鸟的柱子和背景一样的速度移动,会比较舒服,所以我们同样可以采用背景移动的脚本

using System.Collections;using System.Collections.Generic;using UnityEngine;public class MoveObject : MonoBehaviour {    private Rigidbody2D m_rigidbody;    // Use this for initialization    void Start () {        //获取自身的刚体        m_rigidbody = GetComponent<Rigidbody2D>();        //设置刚体的速度        m_rigidbody.velocity = new Vector2(-1.5f, 0);    }    // Update is called once per frame    void Update () {        }}

因为柱子在整个游戏中是不断出现的,为了避免生成柱子,销毁柱子,我们采用对象池的方式,循环利用柱子

using System.Collections;using System.Collections.Generic;using UnityEngine;public class ColumnPools : MonoBehaviour {    //预制体    public GameObject columPrefab;    //对象池的数量    public int colmuPoolsize = 5;    private GameObject[] columcnsPool;    //让预先生成的柱子步子啊摄像机范围内,等我们需要的时候从这里拿出一对放到出生的位置    private Vector2 objectPoolPosition = new Vector2(-17,-10);    //当前柱子的索引    private int currentColumn = 0;    private float timeSinceLastSpawned = 0.0f;    //柱子出生的速度    public float SpawnRate;    //出生点的X轴坐标    private float spawnXPosition = 12f;    //出生点的Y轴坐标    private float spawnYPosition = -3f;    // Use this for initialization    void Start () {        //实例化一个对象池        columcnsPool = new GameObject[colmuPoolsize];        //在对象池中预先实例化好多个柱子        for (int i = 0; i < columcnsPool.Length; i++)        {            columcnsPool[i] = Instantiate(columPrefab, objectPoolPosition, Quaternion.identity);        }    }    // Update is called once per frame    void Update () {        ColumnPositionAndReuse();    }    //柱子的位置设定和重复使用    private void ColumnPositionAndReuse()    {        timeSinceLastSpawned += Time.deltaTime;        if (timeSinceLastSpawned > SpawnRate)        {            //保证柱子出现的时间在3s到4秒之间,避免柱子与柱子之间的距离相等            SpawnRate = Random.Range(3f,4f);            //每次把柱子放在出生点的位置的时候都要把时间清零            timeSinceLastSpawned = 0.0f;            //让柱子的Y方向上有个起伏            spawnYPosition = Random.Range(-3.5f, 0);            //设定出生点的位置吧            columcnsPool[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);            currentColumn++;            if (currentColumn >= colmuPoolsize)            {                currentColumn = 0;            }        }    }}
0 0
原创粉丝点击