###前提・実現したいこと
Unity2Dで、
プレイヤーがゲームオブジェクトを操作して移動させる。
↓
プレイヤーがスペースキーを押したら、その時のゲームオブジェクトの座標を取得する。
↓
取得した座標をConsoleウィンドウに表示する。
ということをしたいです。
###発生している問題・エラーメッセージ
移動させた後、Inspectorウィンドウを見ると、そのゲームオブジェクトの座標が変わっているのに、スペースバーを押すとConsoleウィンドウにゲームオブジェクトを配置した時に設定した値が表示されてしまいます。
###該当のソースコード
座標を取得したいゲームオブジェクト名:sight
sightを配置した時の位置:(0.0 , 0.0)
using UnityEngine; using System.Collections; public class sight : MonoBehaviour { public GameObject num; public Vector2 mun; private GameObject _parent; // Use this for initialization void Start () { transform.parent = GameObject.FindWithTag ("MainCamera").transform; GameObject num = GameObject.FindWithTag ("Player"); _parent = transform.root.gameObject; mun = num.transform.localPosition; Debug.Log ("Parent:" + _parent.name); } // Update is called once per frame void Update () { if(Input.GetKey(KeyCode.UpArrow)){ this.transform.Translate (0f, 0.1f, 0f); } if(Input.GetKey(KeyCode.LeftArrow)){ this.transform.Translate (-0.1f, 0f, 0f); } if(Input.GetKey(KeyCode.RightArrow)){ this.transform.Translate (0.1f, 0f, 0f); } if(Input.GetKey(KeyCode.DownArrow)){ this.transform.Translate (0f, -0.1f, 0f); } if(Input.GetKey(KeyCode.Space)){ print (mun); } } }
###試したこと
調べてみると、transformのPositionは、親からの相対的なローカル座標とあったので、http://qiita.com/hiroyuki7/items/95c66aee26115cf24a19を参考にして、親をメインカメラにして、座標を取得させようとしたのですが、うまく取得できませんでした。おそらく親はメインカメラになっていると思います。
###補足情報(言語/FW/ツール等のバージョンなど)
言語:C#
ツール:Unity 5.3.4 Personal
※これが初めての質問になるので、不足している情報や、書き方の誤りなども一緒に指摘していただけるとありがたいです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/11 09:41