13 [Tooltip(
"Friction to slow down the current velocity")]
14 public float resistance = 0.99f;
15 [Tooltip(
"Apply more resistance when going faster- applied resistance is resistance * (velocity ^ reisistanceVelPower)")]
16 public float resistanceVelPower = 1.5f;
17 [Tooltip(
"Accelerate to goal position at this rate")]
18 public float accelRate = 10f;
19 [Tooltip(
"Apply more acceleration if farther from target- applied acceleration is accelRate + springiness * distance")]
20 public float springiness = 0;
22 [Tooltip(
"Instantly maintain a constant depth from the view point instead of simulating Z-velocity")]
23 public bool SnapZ =
true;
25 private Vector3 velocity;
32 public override void SnapTo(Vector3 position, Quaternion rotation)
34 base.SnapTo(position, rotation);
35 velocity = Vector3.zero;
42 velocity = Vector3.zero;
45 private void CalculateMomentum()
51 var refPos = getRefPos();
52 float goalDepth = (solverHandler.GoalPosition - refPos).magnitude;
53 Vector3 currentDelta = transform.position - refPos;
54 float currentDeltaLen = currentDelta.magnitude;
55 if (!Mathf.Approximately(currentDeltaLen, 0))
57 Vector3 currentDeltaNorm = currentDelta / currentDeltaLen;
58 transform.position += currentDeltaNorm * (goalDepth - currentDeltaLen);
63 Vector3 delta = solverHandler.GoalPosition - transform.position;
64 float deltaLen = delta.magnitude;
67 Vector3 deltaNorm = delta / deltaLen;
69 velocity += deltaNorm * (solverHandler.DeltaTime * (accelRate + springiness * deltaLen));
73 float velMag = velocity.magnitude;
74 if (!Mathf.Approximately(velMag, 0))
76 Vector3 velNormal = velocity / velMag;
77 float powFactor = velMag > 1f ? Mathf.Pow(velMag, resistanceVelPower) : velMag;
78 velocity -= velNormal * (powFactor * resistance * solverHandler.DeltaTime);
81 if (velocity.sqrMagnitude < 0.001f)
83 velocity = Vector3.zero;
87 transform.position += velocity * solverHandler.DeltaTime;
90 private Vector3 getRefPos()
92 if (solverHandler.TransformTarget == null)
96 return solverHandler.TransformTarget.position;