当前位置:网站首页>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 .
边栏推荐
- JS reverse row query data decryption
- Explanation of 34 common terms on the Internet
- JS逆向之巨量创意signature签名
- Three talking about exception -- error handling
- Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
- Don't spend money, spend an hour to build your own blog website
- MySQL: Invalid GIS data provided to function st_ geometryfromtext
- 日本赌国运:Web3.0 ,反正也不是第一次失败了!
- Verification failed, please check your call back website. You can follow the instructions
- I did it with two lines of code. As a result, my sister had a more ingenious way
猜你喜欢

Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)

(7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP

How to modify the error of easydss on demand service sharing time?

Unity SKFramework框架(十五)、Singleton 单例

Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
![[技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术](/img/a7/44609a5acf25021f1fca566c3d8c90.png)
[技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术

Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm

伙伴云表格强势升级!Pro版,更非凡!

三翼鸟两周年:羽翼渐丰,腾飞指日可待

Unity SKFramework框架(十九)、POI 兴趣点/信息点
随机推荐
[技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
JS generates 4-digit verification code
MySQL: Invalid GIS data provided to function st_ geometryfromtext
Find love for speed in F1 delta time Grand Prix
Redis database persistence
Daily question: 1175 Prime permutation
Node.js通过ODBC访问PostgreSQL数据库
Unity skframework framework (XV), singleton singleton
Let juicefs help you with "remote backup"
Lucky numbers in the [leetcode daily question] matrix
Numpy array calculation
Daily practice of C language --- monkeys divide peaches
【云原生数据库】遇到慢SQL该怎么办(上)?
How to modify the error of easydss on demand service sharing time?
Why can't d link DLL
Quantum three body problem: Landau fall
Unity SKFramework框架(十四)、Extension 扩展函数
Record idea shortcut keys
OpenApi-Generator:简化RESTful API开发流程
科技的成就(二十七)