微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

按程序创建 2d 自上而下赛道的最佳方法

如何解决按程序创建 2d 自上而下赛道的最佳方法

我正在尝试创建一个 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()
    {
        
    }
}

谢谢!

enter image description here

enter image description here

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。