生成地图,人物移动

创建地图

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

public class CreatMap : MonoBehaviour
{
    public Texture2D texture;
    public Color td = Color.yellow;
    public Color cd = Color.green;

    public GameObject prefab;
    float p = 0.05f;

    // Start is called before the first frame update
    void Start()
    {
        //生成地图
        texture = new Texture2D(220, 200);
        VertexHelper vh = new VertexHelper();
        for (int x = 0; x < 220; x++)
        {
            for (int z = 0; z < 200; z++)
            {
                float y = Mathf.PerlinNoise(x * p, z * p);
                Color color = Color.Lerp(td, cd, y);
                texture.SetPixel(x, z, color);
                float uvx = x * 1f / (texture.width - 1);
                float uvy = z * 1f / (texture.height - 1);
                vh.AddVert(new Vector3(x, y * 5, z), color, new Vector2(uvx, uvy));
                if (x < 200 && z < 200)
                {
                    vh.AddTriangle(x * 200 + z, x * 200 + z + 1, (x + 1) * 200 + z + 1);
                    vh.AddTriangle(x * 200 + z, (x + 1) * 200 + z + 1, (x + 1) * 200 + z);
                }
            }
        }
        //随机生成植物
        for (int i = 0; i < 3000; i++)
        {
            float x = Random.Range(1, 220);
            float z = Random.Range(1, 220);
            float y = Mathf.PerlinNoise(x * p, z * p) * 5;
            GameObject go = Instantiate(prefab, transform);
            go.transform.localPosition = new Vector3(x, y, z);
        }

        texture.Apply();
        Mesh mesh = new Mesh();
        vh.FillMesh(mesh);
        gameObject.AddComponent<MeshFilter>().mesh = mesh;
        gameObject.AddComponent<MeshCollider>().sharedMesh = mesh;
        Material material = new Material(Shader.Find("Standard"));
        material.mainTexture = texture;
        gameObject.AddComponent<MeshRenderer>().material = material;
        gameObject.AddComponent<NavMeshSourceTag>();

        生成Perlin图片
        //File.WriteAllBytes(Application.dataPath + "/Map/map.png", texture.EncodeToPNG());
        生成mesh网格
        //AssetDatabase.CreateAsset(mesh, "Assets/map.asset");


    }
}

人物移动

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

public class PlayerMove : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out RaycastHit hit))
            {
                if (hit.transform.CompareTag("Terrain"))
                {
                    GetComponent<NavMeshAgent>().SetDestination(hit.point);
                }
            }
        }
    }
}

烘焙地图

摄像机跟随

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值