本篇讲大地图的人物图标瞬移。
瞬移:指的就是点击地图的一个地方,然后角色人物图标可以瞬间移动到这个坐标位置。
效果如图:当在地图中点击白色方块的地方,图标会跳到白色方块。对应的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));
Debug.Log("RAYPOIT" + raypoint);
Ray ray = minimap.minicamera.ViewportPointToRay(raypoint);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
{
minimap.player.position = hit.point;
Debug.Log("位置" + tempVector);
}
}
}
}