本篇讲相同的功能即:点击地图中的一个位置,让图标瞬间移动到点击位置,同时3D场景中人物也可以抵达场景中对应的点击位置。
如图:
操作方法和之前一样:
找到大地图的渲染的Rawimage。然后在它上面添加EventTrigger用来实现交互。接着添加新脚本Click map。
脚本的代码内容如下:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class ClickMap : MonoBehaviour, IPointerClickHandler
{
public testmin minimap;
private Vector2 tempVector;
private Vector2 raypoint;
public void OnPointerClick(PointerEventData eventData)
{
tempVector = new Vector2(eventData.pointerCurrentRaycast.screenPosition.x / Screen.width, eventData.pointerCurrentRaycast.screenPosition.y / Screen.height);
Debug.Log("d点击位置"+tempVector);
//通用分辨率
raypoint = new Vector2((tempVector.x - (((Screen.width - minimap.maxmap.GetComponent<RectTransform>().sizeDelta.x) / 2) / Screen.width)) / (minimap.maxmap.GetComponent<RectTransform>().sizeDelta.x / Screen.width),
(tempVector.y - (((Screen.height - minimap.maxmap.GetComponent<RectTransform>().sizeDelta.y) / 2) / Screen.height)) / (minimap.maxmap.GetComponent<RectTransform>().sizeDelta.x / Screen.height));
Ray ray = minimap.minicamera.ViewportPointToRay(raypoint);//将视图窗口点击位置转化到相机射线
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
{
minimap.player.position = hit.point;
Debug.Log("位置" + tempVector);
}
}
}
}
整体代码需要理解的地方。
一个重要的方法就是: Ray ray = minimap.minicamera.ViewportPointToRay(raypoint);//将视图窗口点击位置转化到相机射线。
//通用分辨率
raypoint = new Vector2((tempVector.x - (((Screen.width - minimap.maxmap.GetComponent<RectTransform>().sizeDelta.x) / 2) / Screen.width)) / (minimap.maxmap.GetComponent<RectTransform>().sizeDelta.x / Screen.width),
(tempVector.y - (((Screen.height - minimap.maxmap.GetComponent<RectTransform>().sizeDelta.y) / 2) / Screen.height)) / (minimap.maxmap.GetComponent<RectTransform>().sizeDelta.x / Screen.height));
掌握了这几点核心就没什么问题了。至此副本地图的功能也讲完了。
标签:minimap,副本,maxmap,GetComponent,Screen,Unity,tempVector,sizeDelta,图标 From: https://blog.51cto.com/u_8378185/5990794