Unity/3d/2dから3dの座標変換
提供: 初心者エンジニアの簡易メモ
void Update() { if (Input.GetMouseButtonDown(0)) { Vector3 screenPoint = Input.mousePosition; GameObject canvas = GameObject.Find("Canvas"); Camera camera = GameObject.Find("Main Camera").GetComponent<Camera>(); RectTransform canvasRect = canvas.GetComponent<RectTransform>(); Vector3 worldPoint = Vector3.zero; RectTransformUtility.ScreenPointToWorldPointInRectangle(canvasRect, screenPoint, camera, out worldPoint); Debug.Log("worldPoint.x=" + worldPoint.x + " y=" + worldPoint.y); GameObject.Find("Whale2").transform.position = new Vector3( worldPoint.x, worldPoint.y, 0); } }