質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Q&A

解決済

1回答

3855閲覧

unityのドラッグについて

m_s

総合スコア51

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

0グッド

1クリップ

投稿2016/06/24 15:46

初心者です
Unityのスクリプトでオブジェクトをドラックさせて移動させたいと思い、他サイトを参考にしましたが出来ていないようでした(ドラックしようとしてもSceneがいつものように動くだけでオブジェクトは動いてくれない)

参考にしたURL1(https://gist.github.com/Buravo46/8367810)
参考にしたURL2(http://neareal.com/1230/)

###とりあえず書いてみたコード

C#

1using UnityEngine; 2using System.Collections; 3 4public class Test : MonoBehaviour { 5 //Rigidbody cubeRb; 6 public float forceY; 7 GameObject plane; 8 GameObject sphere; 9 10//該当コード 11 private Vector3 screenPoint; 12 private Vector3 offset; 13 14 void OnMouseDown() { 15 Debug.Log ("MouseDown"); 16 this.screenPoint = Camera.main.WorldToScreenPoint(transform.position); 17 this.offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); 18 } 19 20 void OnMouseDrag() { 21 Debug.Log ("MouseDrag"); 22 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 23 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 24 transform.position = currentPosition; 25 } 26 27 28 // Use this for initialization 29 void Start () { 30 //cubeRb = this.GetComponent<Rigidbody> (); 31 //forceY = 50; 32 33 plane=GameObject.Find ("Plane"); 34 plane.transform.position = new Vector3 (0,-2,0); 35 //prefabからロード 36 sphere=Resources.Load("Sphere") as GameObject; 37 //Debug.Log (transform.localPosition.x); 38 InvokeRepeating("Instans", 1, 2); 39 } 40 41 42 43 // Update is called once per frame 44 void Update () { 45 //(省略) 46 } 47 48 49}

他サイトを参考にして書いたのですが、なぜそうするのかが分からない部分があるので実際に合っているのか分かりません
解答お願いします!

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

今回ドラッグさせたいオブジェクトは、Start()内で生成されているplanesphereなのでしょうか。
そうだとすると、TestコンポーネントのOnMouseDown()OnMouseDrag()などのドラッグする処理を記述したコンポーネントをplanesphereのオブジェクトにアタッチする必要があると思います。
OnMouseDown()OnMouseDrag()はアタッチしているオブジェクトのコリジョンに対してマウスをクリックしたりドラッグすると呼ばれます。

TestコンポーネントのStart()に記述されているオブジェクト生成などの処理と各オブジェクトのドラッグの処理は別のコンポーネントにしたほうがよいのではないでしょうか。

少なくともTestコンポーネントをColliderと一緒にオブジェクトにアタッチすることでドラッグで移動できることは確認しましたので、ドラッグ処理そのものは機能していると思います。

以下にTestコンポーネントの処理を二つに分けたコンポーネントを利用してドラッグ処理を行ってみました。
ご参考になればと思います。

Generator.cs オブジェクト生成用コンポーネント

csharp

1using UnityEngine; 2 3/// <summary> 4/// オブジェクトの生成などを行う 5/// </summary> 6public class Generator : MonoBehaviour 7{ 8 GameObject sphere; 9 10 void Start() 11 { 12 sphere = GameObject.Instantiate( Resources.Load( "Sphere" ) ) as GameObject; 13 } 14} // class Generator

DragObject.cs ドラッグ処理用コンポーネント

csharp

1using UnityEngine; 2 3/// <summary> 4/// ドラッグ移動するオブジェクト 5/// </summary> 6public class DragObject : MonoBehaviour 7{ 8 private Vector3 screenPoint; 9 private Vector3 offset; 10 11 void OnMouseDown() 12 { 13 Debug.Log( "MouseDown" ); 14 this.screenPoint = Camera.main.WorldToScreenPoint( transform.position ); 15 this.offset = transform.position - Camera.main.ScreenToWorldPoint( new Vector3( Input.mousePosition.x, Input.mousePosition.y, screenPoint.z ) ); 16 } 17 18 void OnMouseDrag() 19 { 20 Debug.Log( "MouseDrag" ); 21 Vector3 currentScreenPoint = new Vector3( Input.mousePosition.x, Input.mousePosition.y, screenPoint.z ); 22 Vector3 currentPosition = Camera.main.ScreenToWorldPoint( currentScreenPoint ) + this.offset; 23 transform.position = currentPosition; 24 } 25 26} // class DragObject

イメージ説明

イメージ説明

投稿2016/06/24 22:26

urahimono

総合スコア714

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問