在我的 2D unity 游戏中,我试图将精灵移动到触摸/光标的位置(现在是单击鼠标)。
我的精灵位于 (173, 48, -52.1) 位置。
然而,当我单击一个可能距离几个像素的位置时,我的位置会更改为 (399, 129, 0),并且我的精灵显然被扔进了广阔的未知世界。
if (Input.GetMouseButtonDown(0))
{
//fingerPos = Input.GetTouch(0).position;
fingerPos = Input.mousePosition;
transform.position = fingerPos;
Debug.Log(transform.position);
}
EDIT
当前代码
if (Input.GetMouseButtonDown (0)) {
fingerPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); //Desktop
transform.position = fingerPos;
Debug.Log (transform.position);
}
Update . Apprantly, when I click the position that is recorded (and the one the sprite is moved to) is directly ON the camera. Refer to image
![Click](https://i.stack.imgur.com/UjA0A.png)
transform.position
位于世界坐标中。Input.mousePosition
返回像素坐标中的值。您需要将其转换为世界坐标。
fingerPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position); //Mobile
fingerPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); //Desktop
然后你就可以将其分配到你的位置。
transform.position = fingerPos;
EDIT:
if (Input.GetMouseButtonDown(0))
{
fingerPos = Input.mousePosition;
fingerPos.z = 10;
fingerPos = Camera.main.ScreenToWorldPoint(fingerPos);
transform.position = fingerPos;
Debug.Log(transform.position);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)