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

質問編集履歴

1

スクリプトを追加しました。

2018/07/15 01:46

投稿

gentome
gentome

スコア12

title CHANGED
File without changes
body CHANGED
@@ -13,5 +13,54 @@
13
13
  Unityのバージョンは2018.1です。
14
14
  2.5Dのゲームを作ろうとしています。
15
15
  Animatorのアバターにはヒューマノイドを使っています。アニメーション自体はアセットストアから入手したものです。
16
+ ソースコードは以下です。Unityの公式リファレンスにあるものを使いました。
17
+ ```C#
18
+ protected Animator animator;
16
19
 
20
+ public bool ikActive = false;
21
+ public Transform rightHandObj = null;
22
+ public Transform lookObj = null;
23
+ void Start()
24
+ {
25
+ animator = GetComponent<Animator>();
26
+ }
27
+
28
+ // IK を計算するためのコールバック
29
+ void OnAnimatorIK()
30
+ {
31
+ if (animator)
32
+ {
33
+
34
+ // IK が有効ならば、位置と回転を直接設定します
35
+ if (ikActive)
36
+ {
37
+
38
+ // すでに指定されている場合は、視線のターゲット位置を設定します
39
+ if (lookObj != null)
40
+ {
41
+ animator.SetLookAtWeight(1);
42
+ animator.SetLookAtPosition(lookObj.position);
43
+ }
44
+
45
+ // 指定されている場合は、右手のターゲット位置と回転を設定します
46
+ if (rightHandObj != null)
47
+ {
48
+ animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1);
49
+ animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1);
50
+ animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandObj.position);
51
+ animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.rotation);
52
+ }
53
+
54
+ }
55
+
56
+ //IK が有効でなければ、手と頭の位置と回転を元の位置に戻します
57
+ else
58
+ {
59
+ animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 0);
60
+ animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 0);
61
+ animator.SetLookAtWeight(0);
62
+ }
63
+ }
64
+ }
65
+ ```
17
66
  情報が少なく恐縮ですがよろしくお願いします。