当前位置:网站首页>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轴的负方向旋转
}
}效果




边栏推荐
- Evil CSRF
- Game Mathematics: calculate the points on the plane in the screen points (God's perspective)
- Market prospect analysis and Research Report of nitrogen liquefier in 2022
- 游戏数学: 计算屏幕点中的平面上的点(上帝视角)
- [激光器原理与应用-2]:国内激光器重点品牌
- d结构用作多维数组的索引
- Market prospect analysis and Research Report of single photon counting detector in 2022
- JVM (3): classloader classification and parental delegation mechanism
- Embedded basic interface-i2c
- JVM(1):介绍、结构、运行和生命周期
猜你喜欢

MySQL锁总结

2022爱分析· 隐私计算厂商全景报告 | 爱分析报告

JVM(6):Slot变量槽、操作数栈、代码追踪、栈顶缓存技术

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

数据类型的转换和条件控制语句

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

Pictures that make people feel calm and warm

SQL注入关联分析

Statistical knowledge required by data analysts

JVM(2):内存结构、类的加载过程
随机推荐
2022-06-10:薯队长从北向南穿过一片红薯地(南北长M,东西宽N),红薯地被划分为1x1的方格, 他可以从北边的任何一个格子出发,到达南边的任何一个格子, 但每一步只能走到东南、正南、西南方向的
Esp32 development -lvgl animation display
Market prospect analysis and Research Report of digital line scan camera in 2022
你知道MallBook分账与银行分账的区别吗?
MySQL stored procedure
Embedded basic interface PWM
域名解析耗时是什么?域名解析耗时影响因素有哪些?
Market prospect analysis and Research Report of beam combiner in 2022
Vulkan-官方示例解读-RayTracing
Eth Of Erc20 And Erc721
B - wall painting (C language)
三层带防护内网红队靶场
JVM(6):Slot变量槽、操作数栈、代码追踪、栈顶缓存技术
Lexical analyzer for compiling principle notes
SQL injection correlation analysis
JVM (1): introduction, structure, operation and lifecycle
Eth Transfer
给你一个项目,你将如何开展性能测试工作?
Use tool classes to read Excel files according to certain rules
Sslstrip Ultimate - location hijacking