当前位置:网站首页>Unity map mapping

Unity map mapping

2022-06-11 04:21:00 Sea moon

Ideas

Location mapping

terrain 2261*3 = 6783

Map 2560

The origin is at the center of the terrain

The mapping scale is (6783/2) / (2560/2) = 3391.5 / 1280 = 2.65

Direction mapping

First find out the player and z Angle between axes

As shown in the figure below

Map the included angle onto the canvas , Let the coordinates point in the corresponding direction

such as , Player pointing x Positive axis , The coordinates should go around z The shaft rotates clockwise in the opposite direction 90 degree

 

Code

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

public class Mapping:MonoSingleton<Mapping>
{
    [Header(" Terrain and map information ")]
    public float terrainHeight;
    public float terrainWidth;
    public float mapHeight;
    public float mapWidth;

    // Mapping ratio 
    float heightConvertRate => terrainHeight / mapHeight;
    float widthConvertRate => terrainWidth / mapWidth;

    public Vector2 GetLocationInMap(Vector3 position)
    {
        return new Vector2(position.x / widthConvertRate, position.z / heightConvertRate);
    }

    public void UpdateLocationOnMap(Vector3 position)
    {
        locationPoint.GetComponent<RectTransform>().anchoredPosition = GetLocationInMap(position);
        locationPoint.transform.rotation = GetRotationFromDirection(player.forward);
    }

    void Update()
    {
        UpdateLocationOnMap(player.position);

        // Consider switching roles , For the time being, I'm lazy , Later, you can call... When switching roles 
        Init();
    }

    // May change in the game 
    Transform player;
    Transform map;
    GameObject locationPoint;
    public void Init()
    {
        if (player == null)
            player = GameObject.FindGameObjectWithTag("Player").transform;

        if (map == null)
            map = GameObject.Find("Map").transform;

        if (locationPoint == null)
        {
            locationPoint = new GameObject("location");
            locationPoint.transform.SetParent(map);
            locationPoint.AddComponent(typeof(Image));
            locationPoint.GetComponent<Image>().sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            locationPoint.GetComponent<RectTransform>().anchoredPosition = GetLocationInMap(player.position);
        }
    }

    // constant 
    public Texture2D texture;
    void Awake()
    {
        Init();
    }

    // Around the z The axis rotates to the given direction 
    public float yaw;
    Quaternion GetRotationFromDirection(Vector3 direction)
    {
        yaw = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;            // Player orientation and z Angle between axes 
        return Quaternion.Euler(0, 0, -yaw);                                    // Wrap around the canvas z The negative rotation of the axis 
    }
}

effect

原网站

版权声明
本文为[Sea moon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110411058171.html