【简单详细】Unity生成地形和UI小地图实时展示人物位置的详细制作过程

效果:

 一:搭建UI

注意:一个父物体Map是一个纯白色image,放到左上角,要加Mask组件哦!!!

      它的两个子物体,其中一个(map)是显示出地图纹理的image,另外一个(mapPlayer)是一个小点,我们也用image来表示,也就是效果图上的绿点

二:玩家

    我们把人物放进去,挂载脚本控制人物移动

public class PlayerControl : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //计算玩家在地形上的高度
        float y = Mathf.PerlinNoise(transform.position.x, transform.position.z) *6;
        //最边的角落上
        transform.position = new Vector3(transform.position.x, y, transform.position.z);
    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(Input.GetAxis("Horizontal") * Vector3.up);
        transform.GetComponent<CharacterController>().Move(transform.forward * Input.GetAxis("Vertical") * 10 * Time.deltaTime);
        transform.GetComponent<CharacterController>().Move(Physics.gravity * Time.deltaTime);
    }

三:生成地形

 创建空物体,挂载脚本,并且给空物体添加一些组件:

这些数值供大家参考,

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

public class TerrianMgr : MonoBehaviour
{
    public float p; //0.1
    public int w, h; //地形宽、长
    public Image mapPlayer, map;
    public Texture2D texture; //地形纹理
    Vector3 leftDownPos = new Vector3(0, 0, 0); //玩家初始位置
    public GameObject player; //玩家

    void Start()
    {
        texture = new Texture2D(w, h);
        BuldMapMesh();
     
    }
    //绘制地形网格过程
    private void BuldMapMesh()
    {
        Mesh mesh = new Mesh();
        VertexHelper vh = new VertexHelper();

        for (int x = 0; x < w; x++)
        {
            for (int z = 0; z < h; z++)
            {
                //柏林噪声
                float noise = Mathf.PerlinNoise(x * p, z * p);
                //根据噪声值计算地形高度
                float y = noise * 5;
                float uvx = (float)x / (float)w;
                float uvy = (float)z / (float)h;
                //颜色的过度
                Color color= Color.Lerp(Color.magenta, Color.blue, noise);
                texture.SetPixel(x, z, color);
                //添加顶点
                vh.AddVert(new Vector3(x, y, z), Color.white, new Vector2(uvx, uvy));
                if(x<w-1&&z<h-1)
                {   
                    //绘制网格 
                    vh.AddTriangle(x * w + z, x * w + z + 1, (x + 1) * w + z + 1);
                    vh.AddTriangle(x * w + z,( x+1) * w + z + 1, (x + 1) * w + z);
                }


            }
        }
       //应用纹理
        texture.Apply();
     
        vh.FillMesh(mesh);
        GetComponent<MeshFilter>().mesh = mesh;
        GetComponent<MeshCollider>().sharedMesh = mesh;
        GetComponent<MeshRenderer>().material.mainTexture = texture;

        //给小地图附上颜色
        Material material = new Material(Shader.Find("UI/Default"));
        material.mainTexture = texture;
        map.GetComponent<Image>().material = material;

    }

    // Update is called once per frame
    void Update()
    {
        //调整map的中心点

        float px = (player.transform.position.x - leftDownPos.x) / w;
        float py = (player.transform.position.z - leftDownPos.z) / h;

        //实时更新锚点,就会实时显示玩家位置
        map.rectTransform.pivot = new Vector2(px, py);

    }
}

四:虚拟相机

这里我添加了虚拟相机,实时跟随玩家移动。

 这样就可以实现效果啦,是不是很简单♡ .^◡^.♡

猜你喜欢

转载自blog.csdn.net/m0_74022070/article/details/130281078