Unity地图中点击角色移动功能
-
效果图
-
demo下载地址 demo下载地址
-
代码
int width = Screen.width; int height = Screen.height; float screenpostionX = eventData.pointerCurrentRaycast.screenPosition.x; float screenpostionY = eventData.pointerCurrentRaycast.screenPosition.y; tempVector = new Vector2(screenpostionX / width, screenpostionY / height); Debug.Log("d点击位置" + tempVector); float Vecx = GetComponent<RectTransform>().sizeDelta.x; float Vecy = GetComponent<RectTransform>().sizeDelta.y; //通用分辨率 raypoint = new Vector2((tempVector.x - ((width - Vecx) / 2 / width)) / (Vecx / width), (tempVector.y - ((height - Vecy) / 2 / height)) / (Vecy / height)); Debug.Log("RAYPOIT" + raypoint); Ray ray = minicamera.ViewportPointToRay(raypoint); RaycastHit hit; if (Physics.Raycast(ray, out hit, Mathf.Infinity)) { target.position = hit.point; Debug.Log("位置" + tempVector); }