初心者です
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}
他サイトを参考にして書いたのですが、なぜそうするのかが分からない部分があるので実際に合っているのか分かりません
解答お願いします!
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。