像素鸟柱子的设置_03

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 initializationvoid Start () {//获取自身的刚体m_rigidbody = GetComponent();//设置刚体的速度m_rigidbody.velocity = new Vector2(-1.5f, 0);}// Update is called once per framevoid 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 initializationvoid 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 framevoid 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;}}}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部