2017-04-01 10 views
-1

マウスでゲームオブジェクトをクリックしてドラッグするにはどうすればよいですか?

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

public class mouseDrag : MonoBehaviour 
{ 
    float distance = 10; 

     void OnMouseDrag() 
     { 
      Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance); 
      Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition); 

      transform.position = objPosition; 
     } 
    } 

が続いてスタート機能で別のスクリプトでは、私は4つのキューブを作成していますし、各キューブにmouseDragを追加する新しいスクリプトを作成しました。しかし、実行してクリックしてドラッグすると何も起こりません。エラーや例外は発生しません。

void Start() 
    { 
     lp = gameObjectToRaise.transform.localPosition; 
     ls = gameObjectToRaise.transform.localScale; 

     List<GameObject> cubes = new List<GameObject>(); 

     GameObject cube = Cube.CreatePrimitive(Cube.CubePivotPoint.UPLEFT); 
     GameObject cube1 = Cube.CreatePrimitive(Cube.CubePivotPoint.UPRIGHT); 
     GameObject cube2 = Cube.CreatePrimitive(Cube.CubePivotPoint.BACKUP); 
     GameObject cube3 = Cube.CreatePrimitive(Cube.CubePivotPoint.UPRIGHT); 

     cubes.Add(cube); 
     cubes.Add(cube1); 
     cubes.Add(cube2); 
     cubes.Add(cube3); 

     cube.GetComponentInChildren<Renderer>().material.color = Color.blue; 
     cube1.GetComponentInChildren<Renderer>().material.color = Color.red; 
     cube2.GetComponentInChildren<Renderer>().material.color = Color.green; 
     cube3.GetComponentInChildren<Renderer>().material.color = Color.yellow; 

     cube.transform.position = new Vector3(lp.x, lp.y, lp.z - 0.5f); 
     cube1.transform.position = new Vector3(lp.x, lp.y, lp.z); 
     cube2.transform.position = new Vector3(lp.x, lp.y, lp.z + 5); 
     cube3.transform.position = new Vector3(lp.x + 5, lp.y, lp.z); 

     cube1.transform.Rotate(0, 90, 0); 
     cube3.transform.Rotate(0, 90, 0); 

     StartCoroutine(scaleCube(cube.transform)); 
     StartCoroutine(scaleCube(cube1.transform)); 
     StartCoroutine(scaleCube(cube2.transform)); 
     StartCoroutine(scaleCube(cube3.transform)); 

     foreach (GameObject go in cubes) 
     { 
      go.AddComponent<mouseDrag>(); 
     } 
    } 

    IEnumerator scaleCube(Transform trans) 
    { 
     while (raiseAmount < raiseTotal) 
     { 
      raiseAmount += 1f; 
      trans.localScale += new Vector3(speed * Time.deltaTime, speed * Time.deltaTime, 0); 
      yield return null; 
     } 
    } 
} 
+0

カメラモードとは何ですか?パースペクティブまたは正書法? – Programmer

+0

@Programmer私はちょうどそれをテストのために追加された単一のキューブgameobjectと今試したとうまくいきます。しかし、私はCUBEクラスヘルパーを使用してキューブを使用しているとき、それは動作していません。たぶん問題は、それが作成されたキューブではなく、parentObject "CubeHolder"にスクリプトを添付していることです。私はゲームを実行しているとき、私は4つの "CubeHolder"の下にチリキューブを見ますが、スクリプトは "CubeHolder"に接続され、4つの "CubeHolder"のそれぞれの子キューブには接続されません。 –

答えて

1

私はちょうど私がそれ をテストするために追加の単一のキューブのゲームオブジェクトを今試み、それが正常に働いています。しかし、CUBE クラスヘルパーを使用しているキューブを使用している場合、それは機能しません。この場合

mouseDragスクリプトはその.After子キューブ(ないCubeHolderオブジェクト)に添付しなければならない、あなたは「CubeHolder」あるキューブの親を移動する必要があります。

子キューブを移動したことがある場合は、ピボットポイントを解除します。

だけで子キューブではない"CubeHolder"mouseDragスクリプトを添付し、その後

transform.parent.position = objPosition; 

transform.position = objPosition; 

を変更。

はたぶん、問題は、それが作成されたキューブへ parentObject「CubeHolder」としないようにスクリプトを付けるということですか?

はい。 OnMouseDragは、Colliderのオブジェクトにアタッチされ、子キューブがColliderの唯一のオブジェクトである場合にのみ呼び出されます。親オブジェクトはピボットポイント機能としてのみ使用されます。

あなたはこのためOnMouseDragを使用しないでください。新しいUIイベントでキューブ/オブジェクトをドラッグする必要があります。答えはthisにリストされているコールバック関数の多くがあります。

以下のスクリプトは、使用する必要があります。 CubeDrag sctiptを子キューブに添付して、transform.positionというものをtransform.parent.positionに変更します。

using UnityEngine; 
using UnityEngine.EventSystems; 
public class CubeDrag: MonoBehaviour, IPointerDownHandler, IDragHandler, IEndDragHandler 
{ 
    Camera mainCamera; 
    float zAxis = 0; 
    Vector3 clickOffset = Vector3.zero; 

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

    public void OnPointerDown(PointerEventData eventData) 
    { 
     Vector3 tempPos = eventData.position; 
     tempPos.z = Vector3.Distance(transform.position, Camera.main.transform.position); 
     clickOffset = transform.position - mainCamera.ScreenToWorldPoint(tempPos); 
     Debug.Log("Mouse Down"); 
    } 

    public void OnDrag(PointerEventData eventData) 
    { 
     Vector3 tempPos = eventData.position; 
     tempPos.z = Vector3.Distance(transform.position, Camera.main.transform.position); 
     Vector3 tempVec = mainCamera.ScreenToWorldPoint(tempPos) + clickOffset; 
     tempVec.z = zAxis; 

     transform.position = tempVec; 
     Debug.Log("Dragging Cube"); 
    } 

    public void OnEndDrag(PointerEventData eventData) 
    { 

    } 

    void addEventSystem() 
    { 
     mainCamera = Camera.main; 
     if (mainCamera.GetComponent<PhysicsRaycaster>() == null) 
      mainCamera.gameObject.AddComponent<PhysicsRaycaster>(); 

     EventSystem eveSys = GameObject.FindObjectOfType(typeof(EventSystem)) as EventSystem; 
     if (eveSys == null) 
     { 
      GameObject tempObj = new GameObject("EventSystem"); 
      eveSys = tempObj.AddComponent<EventSystem>(); 
     } 

     StandaloneInputModule stdIM = GameObject.FindObjectOfType(typeof(StandaloneInputModule)) as StandaloneInputModule; 
     if (stdIM == null) 
      stdIM = eveSys.gameObject.AddComponent<StandaloneInputModule>(); 
    } 
} 
関連する問題