当前位置:网站首页>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




边栏推荐
- Guanghetong 5g module fg650-cn and fm650-cn series are produced in full scale, accelerating the efficient implementation of 5g UWB applications
- JVM (7): dynamic link, method call, four method call instructions, distinguishing between non virtual methods and virtual methods, and the use of invokedynamic instructions
- Code replicates CSRF attack and resolves it
- Guanghetong launched a new generation of 3GPP R16 industrial 5g module fg160 engineering sample
- Rational use of thread pool and thread variables
- Unity 遮挡剔除
- Seven easy-to-use decorators
- Personalized use of QT log module
- 游戏数学: 计算屏幕点中的平面上的点(上帝视角)
- 2022 年 5 月产品大事记
猜你喜欢

Explain in detail the structure and working principle of the crystal oscillator

Vulkan official example interpretation raytracing

Eth Transfer

Guanghetong LTE CAT6 module fm101-cg, which supports CBRS band, took the lead in obtaining FCC certification

Game Mathematics: calculate the points on the plane in the screen points (God's perspective)

Pictures that make people feel calm and warm

Zhongang Mining: fluorochemical industry is the main consumption field of fluorite

Matter protocol

JVM(1):介绍、结构、运行和生命周期

CES 2022 𞓜 guanghetong LTE module fm101 has obtained two important ce/fcc certifications with excellent performance
随机推荐
Exploitation and utilization of clickjacking vulnerability
Ultra simple cameraX face recognition effect package
使用工具类按一定规则读取Excel文件
Unity 在不平坦的地形上创建河流
未來已來,5G-Advanced時代開啟
Golang generics: generics
【CustomView】Glide+BitmapTransformation 图片上下边框波浪处理(WaveTransformation)
Unity 地图映射
零时科技 | Discover 闪电贷攻击事件分析
AI helps release legal potential energy! Release of iterms contract intelligent review system
ESP series module burning firmware
JVM (6): slot variable slot, operand stack, code trace, stack top cache technology
Market prospect analysis and Research Report of electronic pelletizing counter in 2022
27 pieces of advice for communication young people
Unity prefab scene conflict merge tool unityyamlmerge
Evil CSRF
Vulkan official example interpretation shadows (rasterization)
2022 love analysis · privacy computing vendor panoramic report | love Analysis Report
Introduction to the development and production functions of shop facade transfer and rental applet
从初代播种到落地生花,5G商用三周年“催生万物”