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

回答編集履歴

1

上半身・下半身同時ひねり問題への対策を追記

2021/11/01 22:04

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -61,4 +61,66 @@
61
61
 
62
62
  下図のような結果になりました。
63
63
 
64
- ![図](8bfe9efc6fd80aac78b0c6dc46577d4b.gif)
64
+ ![図](8bfe9efc6fd80aac78b0c6dc46577d4b.gif)
65
+
66
+ ##上半身・下半身同時ひねり問題への対策
67
+
68
+ ```lang-csharp
69
+ using UnityEngine;
70
+
71
+ [RequireComponent(typeof(Animator))]
72
+ public class PoseModifier : MonoBehaviour
73
+ {
74
+ [SerializeField][Range(-1.0f, 1.0f)] private float spineFrontBack;
75
+ [SerializeField][Range(-1.0f, 1.0f)] private float chestTwistLeftRight;
76
+ [SerializeField][Range(-1.0f, 1.0f)] private float headNodDownUp;
77
+ private HumanPoseHandler poseHandler;
78
+ private Transform hips;
79
+
80
+ private void OnEnable()
81
+ {
82
+ var animator = this.GetComponent<Animator>();
83
+ this.poseHandler = new HumanPoseHandler(animator.avatar, this.transform);
84
+
85
+ // Hipsに相当するボーンを取得しておく
86
+ this.hips = animator.GetBoneTransform(HumanBodyBones.Hips);
87
+ }
88
+
89
+ private void OnDisable()
90
+ {
91
+ this.poseHandler.Dispose();
92
+ }
93
+
94
+ private void LateUpdate()
95
+ {
96
+ // ポーズ修正を加える前に、あらかじめHipsの回転を覚えておく
97
+ var hipsRotation = this.hips.rotation;
98
+
99
+ // ポーズ修正部分は変更なし
100
+ var t = this.transform;
101
+ var rootPosition = t.position;
102
+ var rootRotation = t.rotation;
103
+ t.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
104
+ var pose = new HumanPose();
105
+ this.poseHandler.GetHumanPose(ref pose);
106
+ t.SetPositionAndRotation(rootPosition, rootRotation);
107
+ pose.muscles[0] += this.spineFrontBack;
108
+ pose.muscles[5] += this.chestTwistLeftRight;
109
+ pose.muscles[12] += this.headNodDownUp;
110
+ this.poseHandler.SetHumanPose(ref pose);
111
+
112
+ // マッスル値設定によるポーズ修正を加えた後、
113
+ // Hipsの回転を元に戻す
114
+ this.hips.rotation = hipsRotation;
115
+ }
116
+
117
+ private void Start()
118
+ {
119
+ var muscleNames = HumanTrait.MuscleName;
120
+ for (var i = 0; i < HumanTrait.MuscleCount; i++)
121
+ {
122
+ Debug.Log($"Muscle {i}:[{muscleNames[i]}]");
123
+ }
124
+ }
125
+ }
126
+ ```