casualhaのブログ

カジュアルゲームのプレイ記録

unity備忘録 JoyStickで移動

JoyStickで移動、アニメーションさせる。

カメラはCinemachineで追尾。

移動はこれ

float x = Joystick.Horizontal * speed;
float z = Joystick.Vertical * speed;

transform.position += new Vector3(x, 0, z);

 

アニメーションの遷移に、parameterを使用。
floatでspeed0.01f以上ならrunのアニメーション、0.01f未満ならidleのアニメーションを再生するよう設定。

f:id:casualha:20210824194823p:plain

 

スクリプトで、xの絶対値とzの絶対値を足したものをspeedにセット。

animator.SetFloat("speed", Mathf.Abs(x) + Mathf.Abs(z));

注意点:Has Exit Timeにチェックが入っていると、アニメーションがすぐに遷移しない。チェックが外れていることを確認。

f:id:casualha:20210824194846p:plain