当前位置:网站首页>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 .
边栏推荐
- What are the classifications of SSL certificates? How to choose the appropriate SSL certificate?
- Embedded software development
- [true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
- 题解:《压缩技术》(原版、续集版)
- Which do you choose between Alibaba P7 with an annual salary of 900000 and deputy department level cadres?
- 每日一题:1175.质数排列
- Web Foundation
- 基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
- Unity SKFramework框架(十四)、Extension 扩展函数
- PR usage skills, how to use PR to watermark?
猜你喜欢

【蓝桥杯选拔赛真题43】Scratch航天飞行 少儿编程scratch蓝桥杯选拔赛真题讲解

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

Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本

Bridge of undirected graph

题解:《压缩技术》(原版、续集版)

Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking

嵌入式软件开发

We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse

Everyone wants to eat a broken buffet. It's almost cold

Redis数据库持久化
随机推荐
nohup命令
Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
Unity SKFramework框架(十九)、POI 兴趣点/信息点
What are the classifications of SSL certificates? How to choose the appropriate SSL certificate?
Web Foundation
JS逆向之行行查data解密
Numpy array calculation
Download files and preview pictures
2022零代码/低代码开发白皮书【伙伴云出品】附下载
Pocket Raider comments
Three talking about exception -- error handling
D为何链接不了dll
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
Unity SKFramework框架(十二)、Score 计分模块
Uniapp develops wechat applet Tencent map function and generates sig signature of location cloud
Unity skframework Framework (XVI), package manager Development Kit Manager
Security RememberMe原理分析
Memory management 01 - link script
Quantum three body problem: Landau fall
口袋奇兵点评