2016-12-25 2 views
-1

안녕하세요 저는 간단한 게임 프로젝트에서 작업 중이며 화면에서 터치하는 위치에서 gameobject를 인스턴스화하려고합니다. 다음 코드를 작성했지만 좌표가 일치하지 않습니다. 그것은 개체를 인스턴스화하지만 난 볼 수 없습니다가 화면에 어디 내가이 문제를 해결하기 위해 그리워 뭔가가 생각 나는 코드가 얼마나 여기 모르는 :유니티 C# 터치 좌표

if (Input.GetTouch(0).phase == TouchPhase.Began) 
     { 

      Debug.Log("touch begun" + Input.GetTouch(0).position); 
      Vector3 touchDeltaPosition = Input.GetTouch(0).position; 
      Instantiate(bulletPrefab, Input.GetTouch(0).position, Quaternion.identity); 

     } 

답변

0

Input.GetTouch의 값 (0). 위치는 화면의 픽셀 좌표입니다. 오브젝트의 인스턴스는 월드 공간 좌표에 있습니다. 월드 공간에서 화면 모서리 좌표를 가져와 터치 값이있는 x 및 y 오프셋을 가져야합니다.

이 밖으로 시도 :

 if (Input.GetTouch(0).phase == TouchPhase.Began) 
     { 
      Vector3 touchData = Input.GetTouch(0).position; 

      touchData.x = x; 
      touchData.y = y; 
      // Construct a ray from the current touch coordinates 
      Ray ray = Camera.main.ScreenPointToRay(new Vector2(touchData.x, touchData.y)); 
      if (Physics.Raycast(ray)) 
      { 
       float desiredValueForZ = 2f; //this depends on your project stuff, I don't know. 
       Instantiate(bulletPrefab, Camera.main.ScreenToWorldPoint(new Vector3(touchData.x, touchData.y, desiredValueForZ)), transform.rotation); 
      } 
     }