###前提・実現したいこと
ある子オブジェクトを違う親オブジェクトの子にして、また元に戻した時に元のTransformから大きく外れ、またlocalPositionで設定し直してもその外れた場所から動きません。原因は何が考えられますか?
目標としては元の親のTransform(0,0,0)に出現し、localPositionなどで位置を移動できれはいいのですが...
イメージとしては以下のような感じです。
1.子A生成
親A
子A
親B
2.移動
親A
親B
子A
3.戻す
親A
子A
親B
###発生している問題・エラーメッセージ
特になし
###該当のソースコード
このコードは実際のコードではないですが、子Aに対する操作は同じです。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class GameManager : MonoBehaviour{ public Transform Atransform; //親A public Transform Btransform; //親B public Button Obj; //子A public Button Pre; void Start(){ Obj = Resources.Load<Button>("Path"); Prefab_create(); ParentMove(); } public void Prefab_create(){ Pre = Instantiate(Obj,Atransform.localPosition,Atransform.localRotation); Pre.transform.SetParent(Atransform); Pre[preCnt].transform.localScale = Vector3.one; } public void ParentMove(){ Obj.transform.SetParent(Btransform); Obj.transform.localPosition = new Vector3(40,40,0); Obj.transform.SetParent(Atransform); Obj.transform.localPosition = new Vector3(60,60,0); } }
###試したこと
追記:例3の「戻す」の場面で、オブジェクトを元の親に戻した後も親Transform内で移動させたいのですが、試したところZ方向だけ指定した値にきちんと動作し、X方向,Y方向についてはどんな値を入れても同じ場所に行きます。(最後のコード)
例えばObj.transform.localPosition = new Vector3(60,60,100);
とするとそのオブジェクトのInspectorでは x 280, y 344 z 100 となります。x、yは何を入れてもこの値になります。
###補足情報(言語/FW/ツール等のバージョンなど)
unity 2017.2.0f3
C#
この例にある親Aと親Bの上にもルートノードがあり、親Aと親Bはその子で、深さ的には同じです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/07 17:06
2017/12/08 08:59
2017/12/08 17:33
2017/12/09 18:13
2017/12/10 07:36
2017/12/10 16:00
2017/12/11 19:40