Get mouse position in world space

天大地大妈咪最大 提交于 2021-01-02 01:57:58

问题


I'm making a game using Unity and I have a little issue, I need to know the mouse position in world space, for that I try to set a GameObject at the mouse position using this code :

    Vector3 p = Input.mousePosition;
    Vector3 pos = Camera.main.ScreenToWorldPoint( p);
    testGameObject.transform.position = pos;

It works like a charm in Editor but in exe / apk, the GameObject dosn't follow the mouse:

Example 1

Example 2

The GameObject that is supposed to follow the mouse is the "1" inside a circle


回答1:


If it's works in editor like a charm, then it should in build to.

I see it already working in build, maybe what you want is to exactly place yor object to match the screen click point, but your object is far too close to the camera so that you can't see it.

maybe there's a problem with the depth of the position from the camera. try adding something like

Vector3 p = Input.mousePosition;
p.z = 20;
Vector3 pos = Camera.main.ScreenToWorldPoint(p);
testGameObject.transform.position = pos;

To add depth to the mouse position. try to change 20 to -20 if it's still doesn't work



来源:https://stackoverflow.com/questions/47253050/get-mouse-position-in-world-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!