using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
//Canvas->RawImage에 넣어서 사용 할 수 있음
public class MiniMapController : MonoBehaviour, IPointerClickHandler
{
public Camera miniMapCam;
public GameObject Player;
public LayerMask mask;
RaycastHit hit;
Ray ray;
public void OnPointerClick(PointerEventData eventData)
{
Vector2 curosr = new Vector2(0, 0);
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent<RawImage>().rectTransform,
eventData.pressPosition, eventData.pressEventCamera, out curosr))
{
Texture texture = GetComponent<RawImage>().texture;
Rect rect = GetComponent<RawImage>().rectTransform.rect;
float coordX = Mathf.Clamp(0, (((curosr.x - rect.x) * texture.width) / rect.width), texture.width);
float coordY = Mathf.Clamp(0, (((curosr.y - rect.y) * texture.height) / rect.height), texture.height);
float calX = coordX / texture.width;
float calY = coordY / texture.height;
curosr = new Vector2(calX, calY);
CastRayToWorld(curosr);
//Player.transform.localPosition = GetClickPos(curosr);
//check();
//MinimapClick();
}
}
private void CastRayToWorld(Vector2 vec)
{
Ray MapRay = miniMapCam.ScreenPointToRay(new Vector2(vec.x * miniMapCam.pixelWidth,
vec.y * miniMapCam.pixelHeight));
RaycastHit miniMapHit;
if (Physics.Raycast(MapRay, out miniMapHit, Mathf.Infinity))
{
Debug.Log("miniMapHit: " + miniMapHit.collider.gameObject);
Player.transform.position = miniMapHit.point;
}
}
}
댓글 없음:
댓글 쓰기