Getting mouse position in unity

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

I'm trying to move a object to the mouse position. But it's giving me large x value like 300 but at that place the pre placed object's x position is -4.

rigidBody.velocity = new Vector3(Input.mousePosition.x, EndPointY, 0)*4; 

So how can I get the current mouse position?

Thank you..

回答1:

That is the current mouse position. The issue is that your objects are in world coordinates and the mouse is using screen coordinates.

You need to convert the mouse position using Camera.ScreenToWorldPoint().



回答2:

Input.mousePosition will give you the position of the mouse on screen (pixels). You need to convert those pixels to the world units using Camera.ScreenToWorldPoint().

You can follow this link to learn how to drag a 3d object with the mouse or you can copy this code to move an object from the current position to the mouse position.

 //the object to move public Transform objectToMove;   void Update()  {      Vector3 mouse = Input.mousePosition;      Ray castPoint = Camera.main.ScreenPointToRay(mouse);      RaycastHit hit;      if (Physics.Raycast(castPoint, out hit, Mathf.Infinity))      {          objectToMove.transform.position = hit.point;      }  } 


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