2017-05-04 16 views
-1

我已經在Vuforia Unity3d中使用了此腳本,但它的行爲很奇怪。如何使用觸摸輸入拖動增強模型

任何人都可以發佈腳本來拖動/移動/旋轉3D模型。

public class touchcontrol : MonoBehaviour { 
    // Use this for initialization 
    void Start() { 

    } 

    // Update is called once per frame 
    void Update() 
    { 

     if (Input.touchCount > 0) 
     { 
      Touch touch = Input.GetTouch(0); // get first touch since touch count is greater than zero 

      if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) 
      { 
       // get the touch position from the screen touch to world point 
       Vector3 touchedPos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10)); 
       // lerp and set the position of the current object to that of the touch, but smoothly over time. 
       transform.position = Vector3.Lerp(transform.position, touchedPos, Time.deltaTime); 
      } 
     } 
    } 
} 

回答

0

您應該如果條件之外移動你的位置更新代碼如下 - 你想達到什麼

public class touchcontrol : MonoBehaviour { 

    public float moveSpeed = 1f; 
    private Vector3 touchedPos; 

    // Use this for initialization 
    void Start() { 
     touchedPos = transform.position; 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     if (Input.touchCount > 0) 
     { 
      Touch touch = Input.GetTouch(0); // get first touch since touch count is greater than zero 

      if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) 
      { 
       // get the touch position from the screen touch to world point 
       touchedPos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, transform.position.z)); 
       // lerp and set the position of the current object to that of the touch, but smoothly over time. 
      } 
     } 
     transform.position = Vector3.Lerp(transform.position, touchedPos, Time.deltaTime * moveSpeed); 
    } 
} 
+0

不,它不起作用它會縮放物體。我必須在觸摸時拖動/旋轉沙發3D模型。 – Kushan2

+0

我想要的東西是這樣的:https://www.youtube.com/watch?v=rSbXR5h5cRc – Kushan2

+0

更新了代碼,這應該將對象移動到觸摸位置而不縮放。 –

1

很簡單,你可以不用編碼。您的問題已經有答案here

+0

你能解釋一下如何使用這個插件嗎?我已經嘗試使用這個youtube.com/watch?v=rSbXR5h5cRc,但在這個版本中沒有找到他在視頻中顯示的文件。 – Kushan2

+0

下載插件,只需打開其中一個演示場景,即可看到它是如何完成的以及使用了哪些腳本。 – Hristo

+0

好的,明白了!但現在我的快速問題是有可能爲一個對象添加更多的腳本?如果是,如何? – Kushan2