「Unity/3d/2dから3dの座標変換」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「<pre> void Update() { if (Input.GetMouseButtonDown(0)) { Vector3 screenPoint = Input.mousePosition; GameObject canvas = GameOb...」) |
(相違点なし)
|
2021年2月24日 (水) 12:11時点における版
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);
}
}
