当前位置:网站首页>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 .
边栏推荐
- 为什么switch 的default后面要跟break?
- Unity SKFramework框架(十四)、Extension 扩展函数
- Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
- [indomitable medal activity] life goes on and writing goes on
- Nohup command
- Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
- MAC (MacOS Monterey 12.2 M1) personal use PHP development
- JS reverse row query data decryption
- Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking
- 科技的成就(二十七)
猜你喜欢
记忆函数的性能优化
OpenFOAM:lduMatrix&lduAddressing
net share
Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
Explanation: here is your UFO, Goldbach conjecture
伙伴云表格强势升级!Pro版,更非凡!
OpenAPI generator: simplify the restful API development process
(6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software
随机推荐
Find love for speed in F1 delta time Grand Prix
Unity SKFramework框架(十九)、POI 兴趣点/信息点
屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
MySQL: Invalid GIS data provided to function st_ geometryfromtext
Explanation of 34 common terms on the Internet
Word efficiency guide - word's own template
Unity skframework framework (XVI), package manager development kit Manager
2、 Frame mode MPLS operation
研究表明“气味相投”更易成为朋友
Why is the default of switch followed by break?
mac(macos Monterey12.2 m1) 个人使用php开发
伙伴云表格强势升级!Pro版,更非凡!
Lucky numbers in the [leetcode daily question] matrix
Download files and preview pictures
Everyone wants to eat a broken buffet. It's almost cold
2022 zero code / low code development white paper [produced by partner cloud] with download
Can automatically update the universal weekly report template, you can use it with your hand!
How much do you know about free SSL certificates? The difference between free SSL certificate and charged SSL certificate
为什么switch 的default后面要跟break?
linux下清理系统缓存并释放内存