当前位置:网站首页>Unity 地图映射
Unity 地图映射
2022-06-11 04:11:00 【海 月】
思路
位置映射


地形 2261*3 = 6783
地图 2560
原点在地形正中心
映射比例为 (6783/2) / (2560/2) = 3391.5 / 1280 = 2.65
方向映射
先求出玩家与z轴的夹角

如下图所示

把夹角映射到画布上,让坐标指向对应的方向
比如,玩家指向x轴正向,坐标就应该绕z轴的反方向顺时针旋转90度

代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Mapping:MonoSingleton<Mapping>
{
[Header("地形及地图信息")]
public float terrainHeight;
public float terrainWidth;
public float mapHeight;
public float mapWidth;
//映射比率
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);
//考虑到切换角色,这里暂时偷个懒,后期可以在切换角色时调用
Init();
}
//在游戏中可能发生变化的
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);
}
}
//不变的
public Texture2D texture;
void Awake()
{
Init();
}
//绕z轴旋转到给定方向
public float yaw;
Quaternion GetRotationFromDirection(Vector3 direction)
{
yaw = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg; //玩家朝向与z轴的夹角
return Quaternion.Euler(0, 0, -yaw); //在画布上绕z轴的负方向旋转
}
}效果




边栏推荐
- Feature selection algorithm based on bare bones particleswarm optimization
- 2022 love analysis · privacy computing vendor panoramic report | love Analysis Report
- 你知道MallBook分账与银行分账的区别吗?
- L'avenir est venu, l'ère 5G - Advanced s'ouvre
- [CF571E] Geometric Progressions——数论、质因数分解
- JVM (4): active and passive use of classes, internal structure of runtime data area, JVM thread description, PC register
- 再聊数据中心网络
- Vulkan-官方示例解读-RayTracing
- Safe and borderless, Guanghe tongdai 5g module +ai intelligent security solution shines at CPSE Expo
- Eth Of Erc20 And Erc721
猜你喜欢

From the first generation of sowing to the first generation of flowers, 5g commercial "gave birth to all things" for the third anniversary

JVM (7): dynamic link, method call, four method call instructions, distinguishing between non virtual methods and virtual methods, and the use of invokedynamic instructions

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

JVM(4):类的主动使用与被动使用、运行时数据区域内部结构、JVM线程说明、PC寄存器

再聊数据中心网络

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

Some differences between people

JVM(7):动态链接、方法的调用、四种方法调用指令区分非虚方法和虚方法、invokedynamic指令的使用

JVM (4): active and passive use of classes, internal structure of runtime data area, JVM thread description, PC register

Programming battle -- challenging college entrance examination questions
随机推荐
邪恶的CSRF
[激光器原理与应用-2]:国内激光器重点品牌
Market prospect analysis and Research Report of seed laser in 2022
The live broadcast helped Hangzhou e-commerce Unicorn impact the listing, and the ledger system restructured the new pattern of e-commerce transactions
JVM (5): virtual machine stack, stack exception, stack storage result and operation principle, stack internal structure, local variable table
Game Mathematics: calculate the points on the plane in the screen points (God's perspective)
Pictures that make people feel calm and warm
Ultra simple cameraX face recognition effect package
Vulkan-官方示例解读-RayTracingShadows&在这里边使用模型(1)
Embedded basic interface-i2c
Source insight 4.0 setting shortcut keys for comments and uncomments
密码找回功能可能存在的问题(补充)
Eth Of Erc20 And Erc721
Construction of esp8266/esp32 development environment
Market prospect analysis and Research Report of nitrogen liquefier in 2022
Watson K's Secret Diary
Market prospect analysis and Research Report of modular lithium ion battery in 2022
How to check whether domain name resolution is effective?
JVM (2): loading process of memory structure and classes
关于重复发包的防护与绕过