Unity学习笔记–无限地图

Unity学习笔记--无限地图(地图拼接)_秋瞑小雁客的博客-CSDN博客_unity无限地图

Unity学习笔记–无限地图
在很多简单的单机跑酷或者赛车游戏中,不需要建立一个很大的地图,可以使用小地图拼接的方式实现无限地图的功能。
具体思想是:玩家处于地图中的某个位置,当快要走出边界时,地图在人物的前方再次生成一个进行拼接,看起来地图是永远跑不完的样子。
unity里面用两个Plane与一个cube做介绍:

在这里插入图片描述
默认的Plane的尺寸为10x10m,两个Plane要在一个目标下;
用Cube代替玩家,沿Z方向拖动cube时,plane会跟随交替生成;

话不多说,上代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AddPlane : MonoBehaviour
{

    const float width = 10f;//这里是平面的宽度
    public Transform player;
    Vector3 initPos;
    void Start()
    {
          initPos = transform.position;
    }

        void Update()
    {
        float totalWidth = width*2f;// * backGroundNum;
        float distZ = player.position.z - initPos.z;//实时计算玩家与插入点的距理
        int n = Mathf.RoundToInt(distZ / totalWidth);//四舍五入一下 

        var pos = initPos;   
        pos.z += n * totalWidth;
        transform.position = pos;
    }
}
 

猜你喜欢

转载自blog.csdn.net/renwen1579/article/details/125884234