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




边栏推荐
- Unity MonoSingleton
- [激光器原理与应用-2]:国内激光器重点品牌
- [server data recovery] data recovery case of RAID5 crash of buddy storage
- Introduction to the development and production functions of shop facade transfer and rental applet
- Guanghetong 5g module shines brightly and has won the "2021 science and technology award of China Electronics Society"
- L'avenir est venu, l'ère 5G - Advanced s'ouvre
- 2022 年 5 月产品大事记
- 数字电影的KDM是什么?
- How to invest in programming knowledge and reduce the impact of knowledge failure
- Embedded basic interface UART
猜你喜欢

众昂矿业:氟化工是萤石的主要消耗领域

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

超简单 CameraX 人脸识别效果封装

Data type conversion and conditional control statements

JVM(3):类加载器分类、双亲委派机制

零时科技 | Discover 闪电贷攻击事件分析

Unity 物品模型旋转展示

Guanghetong LTE Cat4 module l716 is upgraded to provide affordable and universal wireless applications for the IOT industry

MySQL锁总结
![[laser principle and application-2]: key domestic laser brands](/img/55/a87169bb75429f323159e3b8627cc6.jpg)
[laser principle and application-2]: key domestic laser brands
随机推荐
未來已來,5G-Advanced時代開啟
Explain in detail the structure and working principle of the crystal oscillator
Introduction to the development and production functions of shop facade transfer and rental applet
众昂矿业:氟化工是萤石的主要消耗领域
Data type conversion and conditional control statements
JVM(4):类的主动使用与被动使用、运行时数据区域内部结构、JVM线程说明、PC寄存器
AI助力,释放法务势能!iTerms合同智审系统重磅发布
零时科技 | Discover 闪电贷攻击事件分析
QT日志模块的个性化使用
Do you know the difference between mallbook ledger and bank ledger?
数字电影的KDM是什么?
MySQL stored procedure
店铺门面转让出租小程序开发制作功能介绍
Esp32 development -lvgl animation display
d结构用作多维数组的索引
Market prospect analysis and Research Report of denitrification unit in 2022
Vulkan-官方示例解读-Shadows(光栅化)
2022 love analysis · privacy computing vendor panoramic report | love Analysis Report
ESP8266_ RTOS modifies IP address and hostname in AP mode
The live broadcast helped Hangzhou e-commerce Unicorn impact the listing, and the ledger system restructured the new pattern of e-commerce transactions