像素鸟柱子的设置_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 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;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yy763496668

您的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值