回答編集履歴

1

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

2021/11/01 22:04

投稿

Bongo
Bongo

スコア10811

test CHANGED
@@ -125,3 +125,127 @@
125
125
 
126
126
 
127
127
  ![図](8bfe9efc6fd80aac78b0c6dc46577d4b.gif)
128
+
129
+
130
+
131
+ ##上半身・下半身同時ひねり問題への対策
132
+
133
+
134
+
135
+ ```lang-csharp
136
+
137
+ using UnityEngine;
138
+
139
+
140
+
141
+ [RequireComponent(typeof(Animator))]
142
+
143
+ public class PoseModifier : MonoBehaviour
144
+
145
+ {
146
+
147
+ [SerializeField][Range(-1.0f, 1.0f)] private float spineFrontBack;
148
+
149
+ [SerializeField][Range(-1.0f, 1.0f)] private float chestTwistLeftRight;
150
+
151
+ [SerializeField][Range(-1.0f, 1.0f)] private float headNodDownUp;
152
+
153
+ private HumanPoseHandler poseHandler;
154
+
155
+ private Transform hips;
156
+
157
+
158
+
159
+ private void OnEnable()
160
+
161
+ {
162
+
163
+ var animator = this.GetComponent<Animator>();
164
+
165
+ this.poseHandler = new HumanPoseHandler(animator.avatar, this.transform);
166
+
167
+
168
+
169
+ // Hipsに相当するボーンを取得しておく
170
+
171
+ this.hips = animator.GetBoneTransform(HumanBodyBones.Hips);
172
+
173
+ }
174
+
175
+
176
+
177
+ private void OnDisable()
178
+
179
+ {
180
+
181
+ this.poseHandler.Dispose();
182
+
183
+ }
184
+
185
+
186
+
187
+ private void LateUpdate()
188
+
189
+ {
190
+
191
+ // ポーズ修正を加える前に、あらかじめHipsの回転を覚えておく
192
+
193
+ var hipsRotation = this.hips.rotation;
194
+
195
+
196
+
197
+ // ポーズ修正部分は変更なし
198
+
199
+ var t = this.transform;
200
+
201
+ var rootPosition = t.position;
202
+
203
+ var rootRotation = t.rotation;
204
+
205
+ t.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
206
+
207
+ var pose = new HumanPose();
208
+
209
+ this.poseHandler.GetHumanPose(ref pose);
210
+
211
+ t.SetPositionAndRotation(rootPosition, rootRotation);
212
+
213
+ pose.muscles[0] += this.spineFrontBack;
214
+
215
+ pose.muscles[5] += this.chestTwistLeftRight;
216
+
217
+ pose.muscles[12] += this.headNodDownUp;
218
+
219
+ this.poseHandler.SetHumanPose(ref pose);
220
+
221
+
222
+
223
+ // マッスル値設定によるポーズ修正を加えた後、
224
+
225
+ // Hipsの回転を元に戻す
226
+
227
+ this.hips.rotation = hipsRotation;
228
+
229
+ }
230
+
231
+
232
+
233
+ private void Start()
234
+
235
+ {
236
+
237
+ var muscleNames = HumanTrait.MuscleName;
238
+
239
+ for (var i = 0; i < HumanTrait.MuscleCount; i++)
240
+
241
+ {
242
+
243
+ Debug.Log($"Muscle {i}:[{muscleNames[i]}]");
244
+
245
+ }
246
+
247
+ }
248
+
249
+ }
250
+
251
+ ```