-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathIKControl.cs
69 lines (58 loc) · 2.85 KB
/
IKControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using UnityEngine;
[RequireComponent(typeof(Animator))]
public class IKControl : MonoBehaviour {
protected Animator animator;
public bool ikActive = false;
public Transform leftHandObj = null;
public Transform rightHandObj = null;
public Transform headObj = null;
public Transform hipsObj = null;
void Start() {
animator = GetComponent<Animator>();
}
private void LateUpdate() {
if (headObj != null) {
Transform head = animator.GetBoneTransform(HumanBodyBones.Head);
head.rotation = headObj.rotation;
//head.position = headObj.position;
}
if (hipsObj != null) {
Transform hips = animator.GetBoneTransform(HumanBodyBones.Hips);
//hips.rotation = hipsObj.rotation;
Vector3 oldPosition = gameObject.transform.position;
gameObject.transform.position = new Vector3(hipsObj.position.x, oldPosition.y, hipsObj.position.z);
Quaternion oldRotation = gameObject.transform.rotation;
gameObject.transform.rotation = Quaternion.Euler(oldRotation.eulerAngles.x, hipsObj.rotation.eulerAngles.y, oldRotation.eulerAngles.z);
// offset to avoid showing mouth parts in view
gameObject.transform.position += gameObject.transform.forward * -0.1f;
}
}
//a callback for calculating IK
void OnAnimatorIK() {
if (animator) {
//if the IK is active, set the position and rotation directly to the goal.
if (ikActive) {
if (leftHandObj != null) {
animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1);
animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1);
animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandObj.position);
animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandObj.rotation);
}
// Set the right hand target position and rotation, if one has been assigned
if (rightHandObj != null) {
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandObj.position);
animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.rotation);
}
}
//if the IK is not active, set the position and rotation of the hand and head back to the original position
else {
animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 0);
animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 0);
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 0);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 0);
}
}
}
}