-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpringBone.cs
More file actions
135 lines (110 loc) · 3.67 KB
/
Copy pathSpringBone.cs
File metadata and controls
135 lines (110 loc) · 3.67 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
//SpringBone.cs for unity-chan!
//
//Original Script is here:
//ricopin / SpringBone.cs
//Rocket Jump : http://rocketjump.skr.jp/unity3d/109/
//https://twitter.com/ricopin416
//
//Revised by N.Kobayashi 2014/06/20
//
using UnityEngine;
using System.Collections;
namespace UnityChan
{
public class SpringBone : MonoBehaviour
{
//次のボーン
public Transform child;
//ボーンの向き
public Vector3 boneAxis = new Vector3 (-1.0f, 0.0f, 0.0f);
public float radius = 0.05f;
//各SpringBoneに設定されているstiffnessForceとdragForceを使用するか?
public bool isUseEachBoneForceSettings = false;
//バネが戻る力
public float stiffnessForce = 0.01f;
//力の減衰力
public float dragForce = 0.4f;
public Vector3 springForce = new Vector3 (0.0f, -0.0001f, 0.0f);
public SpringCollider[] colliders;
public bool debug = true;
//Kobayashi:Thredshold Starting to activate activeRatio
public float threshold = 0.01f;
private float springLength;
private Quaternion localRotation;
private Transform trs;
private Vector3 currTipPos;
private Vector3 prevTipPos;
//Kobayashi
private Transform org;
//Kobayashi:Reference for "SpringManager" component with unitychan
private SpringManager managerRef;
private void Awake ()
{
trs = transform;
localRotation = transform.localRotation;
//Kobayashi:Reference for "SpringManager" component with unitychan
// GameObject.Find("unitychan_dynamic").GetComponent<SpringManager>();
managerRef = GetParentSpringManager (transform);
}
private SpringManager GetParentSpringManager (Transform t)
{
var springManager = t.GetComponent<SpringManager> ();
if (springManager != null)
return springManager;
if (t.parent != null) {
return GetParentSpringManager (t.parent);
}
return null;
}
private void Start ()
{
springLength = Vector3.Distance (trs.position, child.position);
currTipPos = child.position;
prevTipPos = child.position;
}
public void UpdateSpring ()
{
//Kobayashi
org = trs;
//回転をリセット
trs.localRotation = Quaternion.identity * localRotation;
float sqrDt = Time.deltaTime * Time.deltaTime;
//stiffness
Vector3 force = trs.rotation * (boneAxis * stiffnessForce) / sqrDt;
//drag
force += (prevTipPos - currTipPos) * dragForce / sqrDt;
force += springForce / sqrDt;
//前フレームと値が同じにならないように
Vector3 temp = currTipPos;
//verlet
currTipPos = (currTipPos - prevTipPos) + currTipPos + (force * sqrDt);
//長さを元に戻す
currTipPos = ((currTipPos - trs.position).normalized * springLength) + trs.position;
//衝突判定
for (int i = 0; i < colliders.Length; i++) {
if (Vector3.Distance (currTipPos, colliders [i].transform.position) <= (radius + colliders [i].radius)) {
Vector3 normal = (currTipPos - colliders [i].transform.position).normalized;
currTipPos = colliders [i].transform.position + (normal * (radius + colliders [i].radius));
currTipPos = ((currTipPos - trs.position).normalized * springLength) + trs.position;
}
}
prevTipPos = temp;
//回転を適用;
Vector3 aimVector = trs.TransformDirection (boneAxis);
Quaternion aimRotation = Quaternion.FromToRotation (aimVector, currTipPos - trs.position);
//original
//trs.rotation = aimRotation * trs.rotation;
//Kobayahsi:Lerp with mixWeight
Quaternion secondaryRotation = aimRotation * trs.rotation;
trs.rotation = Quaternion.Lerp (org.rotation, secondaryRotation, managerRef.dynamicRatio);
}
private void OnDrawGizmos ()
{
if (debug) {
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere (currTipPos, radius);
}
}
}
}