-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSpring.cs
43 lines (34 loc) · 1003 Bytes
/
Spring.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
using UnityEngine;
public class Spring {
private float strength;
private float damper;
private float target;
private float velocity;
private float value;
public void Update(float deltaTime) {
var direction = target - value >= 0 ? 1f : -1f;
var force = Mathf.Abs(target - value) * strength;
velocity += (force * direction - velocity * damper) * deltaTime;
value += velocity * deltaTime;
}
public void Reset() {
velocity = 0f;
value = 0f;
}
public void SetValue(float value) {
this.value = value;
}
public void SetTarget(float target) {
this.target = target;
}
public void SetDamper(float damper) {
this.damper = damper;
}
public void SetStrength(float strength) {
this.strength = strength;
}
public void SetVelocity(float velocity) {
this.velocity = velocity;
}
public float Value => value;
}