-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTweenSpringBounce.cs
More file actions
99 lines (87 loc) · 3.56 KB
/
Copy pathTweenSpringBounce.cs
File metadata and controls
99 lines (87 loc) · 3.56 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
// TweenSpringBounce.cs
// Last edited 11:11 AM 04/17/2015 by Aaron Freedman
using UnityEngine;
namespace Assets.PrototypingKit
{
/// <summary>
/// This is a tween that will create spring-like movement based on Hooke's law and
/// Euler integration. It's intended for use with non-physics objects, like sprites,
/// that you don't want to use the Unity rigidbody + collider + spring joint system on.
/// </summary>
public class TweenSpringBounce : MonoBehaviour
{
/// <summary>
/// If set to <c>TRUE</c> the targetPosition will act as a floor for the tween (the attached GameObject)
/// will bounce off the targetPosition and lose velocity equivalent to <c>restitution * velocity</c>.
/// </summary>
public bool targetPositionIsFloor;
[SerializeField] private Vector3 targetPosition, velocity;
[SerializeField] private float dampingRatio;
[SerializeField] private float mass;
[SerializeField] private float k;
[SerializeField] private float restitution;
[SerializeField] private bool clampZ;
[SerializeField] private Vector3 impulse;
public float SpringStrenth
{
set { k = value; }
}
public Vector3 TargetPosition
{
set { targetPosition = value; }
}
private void Start()
{
impulse = Vector3.zero;
}
private void FixedUpdate()
{
/* We're using simple Euler integration to get velocity from accumulated forces created by
* the spring, with damping applied to the velocity
*
* NOTE: Since we're moving the position of the transform you won't want to use this
* tween on an object with a rigidbody.
*/
Vector3 acceleration, normal, positionNextFrame;
Vector3 force = acceleration = Vector3.zero;
force += impulse;
impulse = Vector3.zero;
positionNextFrame = transform.localPosition;
normal = transform.localPosition - targetPosition;
force += -k * normal;
acceleration = (force / mass) * Time.deltaTime;
velocity += acceleration * Time.deltaTime;
velocity *= dampingRatio;
positionNextFrame += velocity * Time.deltaTime;
if (targetPositionIsFloor)
{
if (positionNextFrame.y < 0)
{
velocity *= restitution;
velocity *= -1;
}
positionNextFrame.Set(Mathf.Abs(positionNextFrame.x), Mathf.Abs(positionNextFrame.y), Mathf.Abs(positionNextFrame.z));
}
// check for Z-position clamping to prevent movement between Z-layers
float z = clampZ ? transform.localPosition.z : positionNextFrame.z;
positionNextFrame.z = z;
transform.localPosition = positionNextFrame;
}
/// <summary>
/// Sets the tween values, otherwise sets to default.
/// </summary>
public void SetupTween(float _mass = 1.0f, float _k = 1.0f, float _dampingRatio = 1.0f, float _restitution = 1.0f,
bool _targetPositionIsFloor = false)
{
mass = _mass;
k = _k;
dampingRatio = _dampingRatio;
restitution = _restitution;
targetPositionIsFloor = _targetPositionIsFloor;
}
public void AddImpulse(Vector3 impulseForce)
{
impulse += impulseForce;
}
}
}