2016-12-25 16 views
-1

こんにちは私は単純なゲームプロジェクトに取り組んでいます。画面上のタッチ位置からゲームオブジェクトをインスタンス化したいのですが、次のコードを書きましたが、座標が一致しません。 0(Input.GetTouchの値をユニティ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

:それは、オブジェクトのインスタンス化が、それが画面に表示されている。ここで、iは見カント私はこの問題を解決するために、私は逃す何かがあると思うし、私は、コードがどのようにここではわかりません).positionは画面のピクセル座標です。 オブジェクトのインスタンス化はワールド空間座標にあります。 画面のコーナー座標をワールド空間で取得し、そこから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); 
      } 
     } 
関連する問題