2017-07-28 14 views
0

thisチュートリアルを使用すると、指タップでオブジェクトを表面に配置できます。画面上で指をスワイプしてオブジェクトを配置する方法は?

私たちは、オブジェクトを配置する場所がどこに配置されるべきかを「ペイント」するように、画面上で指をスワイプしながらオブジェクトを配置するようにスクリプトを変更することはできますか?ここで

はチュートリアルから配置するためのスクリプトです:

using UnityEngine; 
using System.Collections; 

public class KittyUIController : MonoBehaviour 
{ 
    public GameObject m_kitten; 
    private TangoPointCloud m_pointCloud; 

    void Start() 
    { 
     m_pointCloud = FindObjectOfType<TangoPointCloud>(); 
    } 

    void Update() 
    { 
     if (Input.touchCount == 1) 
     { 
      // Trigger place kitten function when single touch ended. 
      Touch t = Input.GetTouch(0); 
      if (t.phase == TouchPhase.Ended) 
      { 
       PlaceKitten(t.position); 
      } 
     } 
    } 

    void PlaceKitten(Vector2 touchPosition) 
    { 
     // Find the plane. 
     Camera cam = Camera.main; 
     Vector3 planeCenter; 
     Plane plane; 
     if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) 
     { 
      Debug.Log("cannot find plane."); 
      return; 
     } 

     // Place kitten on the surface, and make it always face the camera. 
     if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f) 
     { 
      Vector3 up = plane.normal; 
      Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized; 
      Vector3 forward = Vector3.Cross(right, plane.normal).normalized; 
      Instantiate(m_kitten, planeCenter, Quaternion.LookRotation(forward, up)); 
     } 
     else 
     { 
      Debug.Log("surface is too steep for kitten to stand on."); 
     } 
    } 
} 

答えて

2

代わりtouchphaseが終了したときに子猫を産卵、あなたはいつでもタッチ移動それらを起動することができます:TouchPhase.Moved。注意してください - これはUpdate()メソッド中に毎フレームチェックされているのでドラッグする間にたくさんの子猫が出現します。時間遅れを追加するか、指が特定の距離を移動した後に産卵することを検討してください。

void Update() 
    { 
     if (Input.touchCount == 1) 
     { 
      // Trigger place kitten function when single touch moves. 
      Touch t = Input.GetTouch(0); 
      if (t.phase == TouchPhase.Moved) 
      { 
       PlaceKitten(t.position); 
      } 
     } 
    } 

距離のチェックはとてもように実装することができます

float Vector3 oldpos; 

void Update() 
{ 
    if (Input.touchCount == 1) 
    { 
     Touch t = Input.GetTouch(0); 
     if (t.phase == TouchPhase.Began) 
     { 
      oldpos = t.position; //get initial touch position 
     } 
     if (t.phase == TouchPhase.Moved) 
     { 
      // check if the distance between stored position and current touch position is greater than "2" 
      if (Mathf.Abs(Vector3.Distance(oldpos, t.position)) > 2f) 
      { 
       PlaceKitten(t.position); 
       oldpos = t.position; // store position for next distance check 
      } 
     } 
    } 
} 
+0

はどうもありがとうございました!あなたが指が動いた距離をチェックする方法についての説明を追加できたら、本当に感謝します!この質問には含まれていませんでしたが、私は子猫をお互いの上に置かないことを確認する必要があるので、私はこの問題を解決するための新しい質問を作成します。 – Rumata

+0

回答が編集され、距離を確認する方法が含まれています。 – ryeMoss

+0

このような詳細な回答をありがとうございました! – Rumata

関連する問題