2

我正在尝试创建一个 2d 自上而下的赛车游戏。每次玩家玩游戏时,这个游戏都会有一个随机的路线图。我考虑过用两种不同的方式来做这件事:瓷砖地图,或者只是通过放置不同的预制件(直路、转弯等)来生成道路。我决定采用预制路线。

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

我只是想知道这是否效率不高,或者我是否认为这是完全错误的。

这里有几张图片显示了我在 Photoshop 中制作的道路瓷砖,然后是一个用于笔直道路的预制件(注意其边缘的对撞机)。

与我想做的类似游戏是 Sling Drift,如果您愿意,我可以提供链接。我不知道添加论坛聊天链接的政策。

另外,这是我的地图生成器代码:

//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()
    {
        
    }
}

谢谢!

在此处输入图像描述 在此处输入图像描述

4

1 回答 1

1

你试过样条吗?它们让您可以轻松地制作像赛道一样的弯曲路径。如果没有,这里有一个视频可能会有所帮助:https ://www.youtube.com/watch?v=7j_BNf9s0jM 。

于 2021-03-23T16:37:50.607 回答