左側に750分スライドするアニメーションなのですが、
線形移動のはずなのにフレームの半分の高さ分667分下にに下がります。
Y座標は同じまま0で設定してます。謎です
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class TabBtn : MonoBehaviour 6{ 7 Animator animator; 8 Vector3 startPos = new Vector3(0,0,0); 9 Vector3 endPos = new Vector3(-750,0,0); 10 public GameObject wrapper; 11 bool testFlg = false; 12 float time = 1; 13 float sum = 0; 14 float wW = 375.0f; 15 16 // Start is called before the first frame update 17 void Start() 18 { 19 //GetComponentを用いてAnimatorコンポーネントを取り出す. 20 animator = GetComponent<Animator>(); 21 } 22 23 // Update is called once per frame 24 void Update() 25 { 26 if (!testFlg) 27 { 28 return; 29 } 30 sum += Time.deltaTime; 31 if (sum <= time) 32 { 33 var rate = sum / time; 34 Debug.Log(startPos); 35 Debug.Log(endPos); 36 Debug.Log(rate); 37 wrapper.transform.position = Vector3.Lerp(startPos, endPos, rate); 38 } else 39 { 40 sum = 0; 41 testFlg = false; 42 } 43 } 44 45 public void changeTab(bool flg) 46 { 47 48 int param = animator.GetInteger("Test"); 49 if (flg) 50 { 51 param = 1; 52 testFlg = true; 53 } 54 else 55 { 56 param = 2; 57 } 58 59 animator.SetInteger("Test", param); 60 } 61} 62
transform.positionとuGUI(Canvas上)の位置はイコールではありません。
「uGUI スクリプト 移動」などで調べてみてください。
ありがとうございます。
localpositionでスライドできましたが、
なぜか今度はGUI側のアンカーがきいてないのか開始位置が右に375分ずれて終了位置が左に-375分ずれてしまいます。
GetComponent<RectTransform>からanchoredPositionをコードで再度 new Vector2(0, 0.5)と設定しましたがずれたままです
状況がよくわかりませんが最初からanchoredPositionのみで制御すればいいのではないでしょうか?
(localpositionはオブジェクトの原点が基準なんでuGUIのアンカーは効きません)
>(localpositionはオブジェクトの原点が基準なんでuGUIのアンカーは効きません)
localpositionは必ずAnchor Presetsのcenter & middleを基準に動くという理解でよろしいですか?
localpositionだと親の中心が原点となるんですね
anchoredPositionを誤解しておりました。
動かしたいUIの中心を原点とするのがanchoredPositionなんですね。
なのでsakura_hanaさんのおっしゃる通りanchoredPositionのみの制御
anchoredPosition = Vector2.Lerp(startPos, endPos, rate);
でうまくいきました
ありがとうございます!!
そうです、positionとlocalpositionは3Dモデルと同様に親(positionの場合はワールド)の中心が原点になります。
anchoredPositionはその名前の通り、Anchorで設定した値が基準となります。(なのでAnchorを変えればUIの中央だけでなく、端を基準にすることも出来ます)
ひとまず解決して何よりです。自己解決として回答投稿をお願いします。

回答1件
あなたの回答
tips
プレビュー