Unity 地形图

通过柏林噪音生成带起伏的地图,生成小地图,实现同步玩家的移动和转向想,相机跟随

1.生成柏林噪音的图片

创建空物体并挂在脚本,在执行一次以后删除空物体或令其失活

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

public class CreatTexture : MonoBehaviour
{
    // Start is called before the first frame update
    int wh = 128;
    void Start()
    {
        Texture2D texture=new Texture2D(wh, wh);
        for(int x  = 0; x < wh; x++)
        {
            for(int y = 0; y < wh; y++)
            {
                float p = Mathf.PerlinNoise(x * 0.1f, y * 0.1f);
                Color color = new Color(p, p, p, 1);
                texture.SetPixel(x, y, color);
            }
        }

        texture.Apply();
        File.WriteAllBytes(Application.dataPath+"/Demo06/map.png",texture.EncodeToPNG());
    }

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

2.生成Plane地形,将生成的图片拖进去,挂上CreatTerrain脚本,生成带有起伏的地形

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

public class CreatTerrain : MonoBehaviour
{
    public int w;
    public int h;
    void Start()
    {
        Texture2D texture = GetComponent<Renderer>().material.mainTexture as Texture2D;
        VertexHelper vh = new VertexHelper();
        w = texture.width;
        h = texture.height;
        for (int x = 0; x <= w; x++)
        {
            for (int z = 0; z <= h; z++)
            {
                float y = texture.GetPixel(x, z).grayscale;
                float uvx = (float)x / texture.width;
                float uvy = (float)z / texture.height;
                vh.AddVert(new Vector3(x-64, y, z-64), Color.white, new Vector3(uvx, uvy));
                if (x < w && z < h)
                {
                    vh.AddTriangle(x * (h + 1) + z, x * (h + 1) + z + 1, (x + 1) * (h + 1) + z + 1);
                    vh.AddTriangle(x * (h + 1) + z, (x + 1) * (h + 1) + z + 1, (x + 1) * (h + 1) + z);
                }
            }
        }

        Mesh mesh = new Mesh();
        vh.FillMesh(mesh);
        GetComponent<MeshFilter>().mesh = mesh;
        GetComponent<MeshFilter>().sharedMesh = mesh;
    }
}

3.创建图片mask,尺寸为300*300,创建子类图像image(表示地图)1000*1000和imgPlayer(代表玩家)50*50

mask要添加mask组件 

 生成代表玩家的游戏对象

4.mask挂上脚本Map 

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

public class Map : MonoBehaviour
{
    public RectTransform image; //地图
    public RectTransform imagePlayer;   //玩家
    public Transform player;  //玩家
    public float wh = 128;

    // Update is called once per frame
    void Update()
    {
        float uvx = (player.position.x + wh / 2) / wh;
        float uvz = (player.position.z + wh / 2) / wh;
        image.pivot=new Vector2(uvx, uvz);//位置同步
        imagePlayer.localEulerAngles=Vector3.back*player.localEulerAngles.y;//朝向同步
    }
}

简单相机跟随:

生成空物体,位置和角度与玩家一直,讲相机设置为其子类,空物体脚本在Update中与玩家同步位置与朝向

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

public class Map : MonoBehaviour
{
    public RectTransform image; //地图
    public RectTransform imagePlayer;   //玩家
    public Transform player;  //玩家
    public float wh = 128;

    // Update is called once per frame
    void Update()
    {
        float uvx = (player.position.x + wh / 2) / wh;
        float uvz = (player.position.z + wh / 2) / wh;
        image.pivot=new Vector2(uvx, uvz);//位置同步
        imagePlayer.localEulerAngles=Vector3.back*player.localEulerAngles.y;//朝向同步
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值