第ä¸ç§ï¼
è¿æ¯ä¸ç§åå°çº¿ï¼è·åå½åç¹å»çç©ä½ï¼å
·æ碰æå¨ï¼çåæ ç¹ï¼ä¹å°±æ¯ç¢°æå¨èå´çåæ ç¹ï¼
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitt = new RaycastHit();
Ray ray =Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hitt);
Debug.Log(hitt.point);
//Debug.Log(Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
第äºç§ï¼
Camera TempC;//å®ä¹æåæºåé
private void Start()
{
TempC = this.GetComponent<Camera>();//è·åæåæºçç»ä»¶ Camera
}
void Update()
{
if(Input.GetMouseButtonDown(0))
{
Debug.Log(TempC.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, 10.0f)));
}
}
注æï¼è¿é容æåºç°çé®é¢æ¯ï¼ä»é¼ æ åæ ç¹è½¬æä¸çåæ ç¹çæ¶åï¼æ°çä¸ç»´åéZè½´çèµå¼å¿
é¡»ä¸è½æ¯0.0fï¼å 为ï¼å±å¹å æ ç¹çZè½´ï¼å
¶å®æ¯ç¸å¯¹äºå½åæåæºçï¼å¦ææ¯0ï¼åªè½è½¬ä¸æ¬¡ä¸çåæ ç³»ï¼å©ä¸çä¸ç®¡ç¹åªéé½æ¯ä¸ä¸ªå¼ï¼æ以ä¸è½æZå¼è®¾ä¸ºåæåæºçZåæ éåï¼æ们éè¦è®¾ç½®ä¸ä¸ªé0çæ°åï¼æå转æä¸çåæ ç³»åçå¼æ¯ï¼æåæºçZè½´å ä¸æ设置çå¼ãä¾å¦å½åæåæºZåæ æ¯10fï¼ææ设置çZå¼æ¯10fï¼æå转æä¸çåæ ç³»åçZå¼å°±æ¯20ï¼ä¸é¢æ¯èå¤çç»å
¸åå¤ã
Thereare two problems here. The first one is that you need a 'new' in front of yourVector3() in C#. The second is that the 'z' parameter must be the distance infront of the camera. In perspective mode at least, passing 0 will causeScreenToWorldPoint() to always return the position of the camera. So the callwill look something like:
worldPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, 10.0f));
Notethe measurement for the 'z' parameter is from camera plane to the playingsurface plane. It is not the distance between the camera and the object. If thecamera is aligned with the axes, then the distance is easy to calculate. If thecamera is at an angle (i.e. not axes aligned), then ScreenToWorldPoint() is notthe way to go.
温馨提示:答案为网友推荐,仅供参考