teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

StateMachineBehaviourによる方法を追記

2021/07/18 01:10

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -30,4 +30,30 @@
30
30
  animator.ApplyBuiltinRootMotion();
31
31
  }
32
32
  }
33
- ```
33
+ ```
34
+
35
+ ## StateMachineBehaviourによるApply Root Motion切り替え
36
+
37
+ まず、当初の回答で追加したキャラクター用のスクリプトの`OnAnimatorMove`は削除してしまい、再び「歩行アニメーションを再生すれば前進し、ジャンプアニメーションを再生すれば上方へ跳び上がる」という状態にしておきます。
38
+ そして、下記のようなスクリプトを作り...
39
+
40
+ ```lang-csharp
41
+ using UnityEngine;
42
+
43
+ public class PartialRootMotionDisabler : StateMachineBehaviour
44
+ {
45
+ public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
46
+ {
47
+ animator.applyRootMotion = false;
48
+ }
49
+
50
+ public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
51
+ {
52
+ animator.applyRootMotion = true;
53
+ }
54
+ }
55
+ ```
56
+
57
+ それをApply Root Motionオフ状態にしたいステートにアタッチします。これならいかがでしょうか。
58
+
59
+ ![図](ee2af9bef73f8f7867afc637bed2ce63.png)