Unity3d 按程序创建二维自上而下赛道的最佳方法

Unity3d 按程序创建二维自上而下赛道的最佳方法,unity3d,procedural,topdown,Unity3d,Procedural,Topdown,我正在尝试创建一个2d自上而下的赛车游戏。这个游戏将有一个随机的路线图,每次玩家玩游戏。我考虑过用两种不同的方法来实现这一点:一种是平铺贴图,另一种是通过放置不同的预制件(直路、转弯等)来生成道路。我决定走预制路线 我认为它应该起作用的方式是在预制方形“瓷砖”的边缘设置自己的碰撞器,这样我就可以判断玩家是否偏离了赛道,在这种情况下他们会爆炸。我将有一个MapGenerator脚本,它将通过跟踪最后放置的瓷砖(包括其位置和道路类型:左转、直行、右行等)来生成初始随机地图。当玩家越来越接近终点时,这

我正在尝试创建一个2d自上而下的赛车游戏。这个游戏将有一个随机的路线图,每次玩家玩游戏。我考虑过用两种不同的方法来实现这一点:一种是平铺贴图,另一种是通过放置不同的预制件(直路、转弯等)来生成道路。我决定走预制路线

我认为它应该起作用的方式是在预制方形“瓷砖”的边缘设置自己的碰撞器,这样我就可以判断玩家是否偏离了赛道,在这种情况下他们会爆炸。我将有一个MapGenerator脚本,它将通过跟踪最后放置的瓷砖(包括其位置和道路类型:左转、直行、右行等)来生成初始随机地图。当玩家越来越接近终点时,这个脚本将继续随机添加到道路上,这使它成为一条无限的道路

我只是想知道这是不是效率低下,还是我认为这是完全错误的

这里有两张图片显示了我在photoshop中制作的道路瓷砖,然后是一条直线道路的预制件(注意边缘的碰撞器)

一个类似的游戏,我想做的是吊索漂移,我可以提供链接,如果你想。我不知道在论坛聊天中添加链接的政策

此外,以下是我的地图生成器代码:

//Type of tyle, types are normal (straight road or horizontal road) and turns 
public enum MapTileType
{
    NORMAL,
    N_E,
    N_W,
    S_E,
    S_W
}

//structure for holding the last tile location and its type. 
public struct TypedTileLocation
{
    public TypedTileLocation(Vector2 pos, MapTileType tyleType)
    {
        m_tileType = tyleType;
        m_position = pos;
    }
    public Vector2 m_position;
    public MapTileType m_tileType;

}

public class MapGenerator : MonoBehaviour
{

    //Map Tiles
    public GameObject m_roadTile;
    public GameObject m_turnNorthWestTile;

    //holds all the tiles made in the game
    private List<GameObject> m_allTiles;

    //Map Tile Widths and Height
    private float m_roadTileWidth, m_roadTileHeight;

    //Used for generating next tile
    TypedTileLocation m_lastTilePlaced;

    private void Awake()
    {
        //store the initial beginning tile location (0,0)
        m_lastTilePlaced = new TypedTileLocation(new Vector2(0,0), MapTileType.NORMAL);

        //set height and width of tiles
        m_roadTileWidth = m_roadTile.GetComponent<Renderer>().bounds.size.x;
        m_roadTileHeight = m_roadTile.GetComponent<Renderer>().bounds.size.y;

        m_allTiles = new List<GameObject>();

    }

    // Start is called before the first frame update
    void Start()
    {
        SetupMap();
    }

    void SetupMap()
    {
        //starting at the beginning, just put a few tiles in straight before any turns occur
        for (int i = 0; i < 6; ++i)
        {
            GameObject newTempTile = Instantiate(m_roadTile, new Vector2(0, m_roadTileHeight * i), Quaternion.identity);
            m_lastTilePlaced.m_tileType = MapTileType.NORMAL;
            m_lastTilePlaced.m_position.x = newTempTile.transform.position.x;
            m_lastTilePlaced.m_position.y = newTempTile.transform.position.y;

            m_allTiles.Add(newTempTile);
        }

        //now lets create a starter map of 100 road tiles (including turns and straigt-aways) 
        for (int i = 0; i < 100; ++i)
        {
            //first check if its time to create a turn. Maybe I'll randomly choose to either create a turn or not here

            //draw either turn or straight road, if the tile was a turn decide which direction we are now going (N, W, E, S).
            //this helps us determine which turns we can take next

            //repeat this process.
        }
    }

    void GenerateMoreMap()
    {
        //this will generate more map onto the already existing road and then will delete some of the others
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void OnDrawGizmos()
    {
        
    }
}
//类型类型为正常(直道或水平道)和转弯
公共枚举映射类型
{
正常,,
嗯,,
嗯,,
S_E,
S_W
}
//用于固定最后一个瓷砖位置及其类型的结构。
公共结构类型文件位置
{
公共类型文件位置(Vector2位置,MapTileType类型)
{
m_tileType=tyleType;
m_位置=位置;
}
公共向量2 m_位置;
公共MapTileType m_tileType;
}
公共类映射生成器:MonoBehavior
{
//地图分幅
公共游戏对象m_roadTile;
公共游戏对象m_turnNorthWestTile;
//保存游戏中制作的所有瓷砖
私有列表m_allTiles;
//贴图平铺宽度和高度
私人浮动m_Roadwidth,m_Roadwidth;
//用于生成下一个磁贴
键入文件位置m_last tileplaced;
私人空间
{
//存储初始起始磁贴位置(0,0)
m_lastTilePlaced=新类型的文件位置(新向量2(0,0),MapTileType.NORMAL);
//设置瓷砖的高度和宽度
m_roadTileWidth=m_roadTile.GetComponent().bounds.size.x;
m_roadTileHeight=m_roadTile.GetComponent().bounds.size.y;
m_allTiles=新列表();
}
//在第一帧更新之前调用Start
void Start()
{
SetupMap();
}
void SetupMap()
{
//从一开始,在任何转弯之前,只需将几块瓷砖放直即可
对于(int i=0;i<6;++i)
{
GameObject newEmptFile=实例化(m_roadTile,新矢量2(0,m_roadTileHeight*i),四元数.identity);
m_lastTilePlaced.m_tileType=MapTileType.NORMAL;
m_lastTilePlaced.m_position.x=newtEmptFile.transform.position.x;
m_lastTilePlaced.m_position.y=newTempTile.transform.position.y;
m_allTiles.Add(newTempTile);
}
//现在,让我们创建一个包含100个道路瓷砖的起始地图(包括转弯和斜道)
对于(int i=0;i<100;++i)
{
//首先检查是否是创建回合的时间。也许我会随机选择创建回合或不创建回合
//绘制转弯或直道,如果瓷砖是转弯,请确定我们现在要走的方向(N、W、E、S)。
//这有助于我们确定下一步可以采取哪些措施
//重复这个过程。
}
}
void GenerateMoreMap()
{
//这将在现有道路上生成更多地图,然后删除一些其他地图
}
//每帧调用一次更新
无效更新()
{
}
private void OnDrawGizmos()
{
}
}
谢谢


你试过样条曲线吗?它们可以让你轻松地做出像赛道一样的曲线。如果没有,这里有一段视频可能会有所帮助: