当前位置:网站首页>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 .
边栏推荐
- 科技的成就(二十七)
- 文件的下载与图片的预览
- Runhe hi3516 development board openharmony small system and standard system burning
- Unity SKFramework框架(十四)、Extension 扩展函数
- D为何链接不了dll
- Android kotlin broadcast technology point
- EasyDSS点播服务分享时间出错如何修改?
- 运维必备——ELK日志分析系统
- Achievements in science and Technology (27)
- Which do you choose between Alibaba P7 with an annual salary of 900000 and deputy department level cadres?
猜你喜欢
【笔耕不辍勋章活动】生命不止,写作不息
Gee learning notes 2
Jerry's watch stops ringing [article]
Bridge of undirected graph
Unity SKFramework框架(十六)、Package Manager 開發工具包管理器
Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
Unity skframework framework (XIV), extension extension function
mac(macos Monterey12.2 m1) 个人使用php开发
Unity SKFramework框架(十六)、Package Manager 开发工具包管理器
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
随机推荐
Error function ERF
Record idea shortcut keys
Android kotlin fragment technology point
无向图的桥
A better database client management tool than Navicat
[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
Jerry's watch delete alarm clock [chapter]
Unity SKFramework框架(十四)、Extension 扩展函数
Winter vacation daily question - lucky numbers in the matrix
JS逆向之行行查data解密
Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
Unity SKFramework框架(十六)、Package Manager 开发工具包管理器
2022 Heilongjiang provincial examination on the writing skills of Application Essays
Solve "sub number integer", "jump happily", "turn on the light"
Student course selection information management system based on ssm+jsp framework [source code + database]
诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...
leetcode621. task scheduler
Unity skframework framework (XX), VFX lab special effects library
Fundamentals of face recognition (facenet)
SAP MM 因物料有负库存导致MMPV开账期失败问题之对策