当前位置:网站首页>Unity small map production [2]
Unity small map production [2]
2022-07-02 13:38:00 【Mercury Note】
The project team has added some advanced functions to the small map module , Here is a record of the requirements and production process
demand

The following figure shows the effect of my understanding of requirements errors , When the touch is outside the map , The actual position of the camera should be the same as the blue dot square in the figure , This is the center of the map , Blue dot square , Touch point three o'clock line , I feel that this situation may be useful in the future , So keep .

Ideas
On the basis of one , take UI The local coordinates of the mapping object are limited . Because the actual map was originally made by UI Remapping by mapping the local coordinates of the object .
Code
Time relationship directly give the code ,Test1 Just phased ,Test2 Is the final version of the experiment
The problems encountered are also commented in the code , And this one demo Only applicable to large maps and UI Of mapping objects pivot All are 0.5,0.5 The situation of , In other cases, you should calculate the rectangle by yourself Rect In the center of
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test1 : MonoBehaviour
{
[SerializeField]
private RectTransform mapRect;
[SerializeField]
private RectTransform smallMapRect;
[SerializeField]
private bool onlyMarginOffset;
[SerializeField]
private float offset;
[SerializeField]
private float currentOffset;
// Start is called before the first frame update
void Start()
{
if (onlyMarginOffset)
{
// Try one Use the diagonal length as the offset value Performance is not what you want
float halfDiagonal = Mathf.Sqrt(Mathf.Pow(smallMapRect.rect.width / 2f, 2) + Mathf.Pow(smallMapRect.rect.height / 2f, 2));
currentOffset = halfDiagonal;
}
else
{
// Try two Use a fixed value as the offset value Performance is not what you want
currentOffset = offset;
}
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
bool isIn = RectTransformUtility.RectangleContainsScreenPoint(mapRect, Input.mousePosition);
print(" isIn " + isIn);
if (!isIn)
{
// Pay attention to the difference between this paragraph and the next paragraph , It should be noted that
//1. Coordinate system , All coordinates involved in ray and assignment need to be in the same coordinate system , That makes sense Be clear Overlay Of Canvas and Camera Of Canvas Of position The difference between The current script is only applicable to Overlay
//2. If the ray is from Collider From inside , Then the point of collision can only be the launch point , This is Unity Of Raycast Of API The rules of ,
// The rule is to carry out step-by-step detection along the path from the starting point to the end point of the ray , Once a point is detected at a Collider Inside , Stop the detection , And return collision information
// So it's a more accurate method of judging whether it's done internally or not to the edge
// So the introduction Physics2D.Raycast The ray formed by the parameter of must be from collider From the outside to the inside
// Vector3 dir = Input.mousePosition - mapRect.position;
// RaycastHit2D hit = Physics2D.Raycast(mapRect.position, dir);
Vector3 dir = mapRect.position - Input.mousePosition;
RaycastHit2D hit = Physics2D.Raycast(Input.mousePosition, dir);
if (hit.collider != null)
{
// Try one and two Code for
Vector3 dirVector = new Vector3(hit.point.x , hit.point .y, mapRect.position.z) - mapRect.position;
Vector3 normalizedDirVector = dirVector.normalized;
float newMagnitude = (dirVector.magnitude - currentOffset);
Vector3 newDirVector = normalizedDirVector * newMagnitude;
smallMapRect.position = mapRect.position + newDirVector;
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test2 : MonoBehaviour
{
[SerializeField]
private RectTransform mapRect;
[SerializeField]
private RectTransform smallPointRect;
private BoxCollider2D smallPointCollider2D;
private BoxCollider2D mapCollider2D;
private Vector2 lastHitMapBorderPos;
private bool rayCastNextFrame;
// Start is called before the first frame update
void Start()
{
smallPointCollider2D = smallPointRect.GetComponent<BoxCollider2D>();
mapCollider2D = mapRect.GetComponent<BoxCollider2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
bool isIn = RectTransformUtility.RectangleContainsScreenPoint(mapRect, Input.mousePosition);
print(" isIn " + isIn);
if (!isIn)
{
// Option three
// Diagonal judgment
Vector3 intoMapDir = mapRect.position - Input.mousePosition;
smallPointCollider2D.enabled = false;
mapCollider2D.enabled = true;
RaycastHit2D hit = Physics2D.Raycast(Input.mousePosition, intoMapDir);
// Option four
if (hit.collider != null)
{
smallPointRect.position = hit.point;
mapCollider2D.enabled = false;
smallPointCollider2D.enabled = true;
Vector3 outOfMapDir = Input.mousePosition - mapRect.position ;
lastHitMapBorderPos = hit.point;
RaycastHit2D hit2 = Physics2D.Raycast(mapRect.position, outOfMapDir);
if (hit2.collider != null )
{
float minusDeltaDistance = (hit2.point - hit.point).magnitude;
Vector2 normalizedDir = (hit.point - hit2.point).normalized;
Vector2 currentDirVector = new Vector3(hit.point.x, hit.point.y, mapRect.position.z) - mapRect.position;
float actualMagnitude = currentDirVector.magnitude - minusDeltaDistance;
Vector2 optimizedVector = actualMagnitude * normalizedDir;
Vector2 optimizedPos = mapRect.position + new Vector3(optimizedVector.x, optimizedVector.y, 0);
smallPointRect.position =
new Vector3(optimizedPos.x, optimizedPos.y, smallPointRect.position.z);
}
}
}
}
}
}
here Test2 At that time, scheme 3 was like this , Divide the center point of the rectangle and the four corners of the rectangle into four triangles , Judge the center point 360 The angle divided by these four triangles , According to the angle between the vector from the mouse to the center point and a line of the four triangles in the picture , Determine which triangle it belongs to , Then, according to the triangle, the edges of the large map are directly pushed inward, and the length of the width of the small map is limited . But the feeling is too complicated , We adopted plan 4
I encountered a problem of non response on the way , It was thought that the range update of the collision body would not be effective until the next frame after setting the position , It is found that the collision body is not opened , Then it's all right .
There are other collision bodies and I don't want to add layer Under the circumstances , use RayCastAll Add traversal to see whether the name of the collision object is the name of the specified object .
边栏推荐
- Let juicefs help you with "remote backup"
- Everyone wants to eat a broken buffet. It's almost cold
- Solve "sub number integer", "jump happily", "turn on the light"
- Essential for operation and maintenance - Elk log analysis system
- 2022零代码/低代码开发白皮书【伙伴云出品】附下载
- Unity skframework framework (XV), singleton singleton
- 挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
- Solution: Compression Technology (original version and sequel version)
- Japan bet on national luck: Web3.0, anyway, is not the first time to fail!
- Unity skframework framework (XII), score scoring module
猜你喜欢

Daily practice of C language --- monkeys divide peaches

不会看器件手册的工程师不是个好厨子

Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
![Student course selection information management system based on ssm+jsp framework [source code + database]](/img/71/900d83dba41974589b15d23e632119.png)
Student course selection information management system based on ssm+jsp framework [source code + database]

A better database client management tool than Navicat

Unity skframework framework (XII), score scoring module

Redis数据库持久化

挥发性有机物TVOC、VOC、VOCS气体检测+解决方案

Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具

屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
随机推荐
Performance optimization of memory function
[cloud native database] what to do when encountering slow SQL (Part 1)?
Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
中文姓名提取(玩具代码——准头太小,权当玩闹)
【OpenGL】笔记二十九、高级光照(镜面高光)
Verification failed, please check your call back website. You can follow the instructions
linux下清理系统缓存并释放内存
验证失败,请检查您的回电网址。您可以按照指导进行操作
Download files and preview pictures
Lucky numbers in the [leetcode daily question] matrix
日本赌国运:Web3.0 ,反正也不是第一次失败了!
Bridge of undirected graph
OpenAPI generator: simplify the restful API development process
[youcans' image processing learning course] general contents
How to modify the error of easydss on demand service sharing time?
A better database client management tool than Navicat
能自动更新的万能周报模板,有手就会用!
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
题解:《压缩技术》(原版、续集版)
What are eNB, EPC and PGW?