2017-10-17 145 views
-1

嗨,我跟着一個關於如何通過拖動來移動精靈的utube教程,但我無法讓它工作。用鼠標點擊移動精靈

我是新來的團結很抱歉,如果這是一個有點簡單

這是貼在我的主攝像頭的劇本,我重視的膠囊colllider到我的精靈

感謝您對您的幫助先進

下一步是添加觸摸輸入

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

    public class DragMove : MonoBehaviour { 

    public GameObject gameObjectToDrag; // refer to Go that being dragged 

    public Vector3 Gocenter; // gameobject centre 
    public Vector3 touchPosition; // touch or click position 
    public Vector3 offSet; // vector between touchpoint/mouse click to the  object centre 
    public Vector3 newGOCenter; // new center of object 

    RaycastHit hit; // store hit object information 

    public bool draggingmode = false; // 



// Use this for initialization 
void Start() { 

} 

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

    //******************************** 
    // **Click to Drag**************** 
    //******************************** 

    // first frame when user click left button 

    if (Input.GetMouseButtonDown(0)) 
    { 
     // convert mouse position to ray 
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 

     // if ray hit a collider (not 2dcollider) 
     if (Physics.Raycast(ray, out hit)) 
     { 
      gameObjectToDrag = hit.collider.gameObject; 
      Gocenter = gameObjectToDrag.transform.position; 
      touchPosition = Camera.main.ScreenToWorldPoint 
(Input.mousePosition); 
      offSet = touchPosition - Gocenter; 
      draggingmode = true; 
     } 
    } 

    // every frame when user hold left mouse 
    if (Input.GetMouseButtonDown(0)) 
    { 
     if (draggingmode) 
     { 
      touchPosition = Camera.main.ScreenToWorldPoint 
(Input.mousePosition); 
      newGOCenter = touchPosition - offSet; 
      gameObjectToDrag.transform.position = new Vector3(newGOCenter.x, 
newGOCenter.y, newGOCenter.z); 
     } 
    } 
    if (Input.GetMouseButtonUp(0)) 
    { 
     draggingmode = false; 
    } 

    } 
} 
+0

我點擊儘管如此,但不動 –

+0

你確定這是一個精靈?請張貼「Inspector」選項卡的該對象的屏幕截圖 – Programmer

回答

0

GetMouseButtonDown僅返回true第一幀。 GetMouseButton用於檢測何時按住鼠標按鈕。

// every frame when user hold left mouse 
if (Input.GetMouseButtonDown(0)) 
{ 

應改爲閱讀

// every frame when user hold left mouse 
if (Input.GetMouseButton(0)) 
{ 
+0

謝謝你不好的複製 –