19 [Header(
"Transform targets")]
21 private Transform positionTarget = null;
24 private Transform rotationTarget = null;
27 private Transform distortionTarget = null;
30 private Transform attachPointOffset = null;
32 [Header(
"Blob settings")]
35 private float blobInertia = 0.5f;
39 public float BlobInertia
47 blobInertia = Mathf.Clamp(value, 0, maxInertia);
53 private float blobDistortion = 0.1f;
57 public float BlobDistortion
61 return blobDistortion;
65 blobDistortion = Mathf.Clamp(value, 0, maxDistortion);
71 private float blobRotation = 0.1f;
75 public float BlobRotation
83 blobRotation = Mathf.Clamp(value, 0, maxRotation);
89 private float positionCorrectionStrength = 1f;
93 public float PositionCorrectionStrength
97 return positionCorrectionStrength;
101 positionCorrectionStrength = Mathf.Clamp(value, minPositionCorrection, maxPositionCorrection);
107 private float distortionCorrectionStrength = 1f;
111 public float DistortionCorrectionStrength
115 return distortionCorrectionStrength;
119 distortionCorrectionStrength = Mathf.Clamp(value, minDistortionCorrection, maxDistortionCorrection);
125 private float rotationCorrectionStrength = 1f;
129 public float RotationCorrectionStrength
133 return rotationCorrectionStrength;
137 rotationCorrectionStrength = Mathf.Clamp(value, minRotationCorrection, maxRotationCorrection);
142 private Vector3 blobOffset;
146 public Vector3 BlobOffset
158 private const float maxInertia = 5f;
159 private const float maxDistortion = 1f;
160 private const float maxRotation = 1f;
161 private const float minPositionCorrection = 0.1f;
162 private const float minDistortionCorrection = 0.1f;
163 private const float minRotationCorrection = 0.1f;
164 private const float maxPositionCorrection = 5f;
165 private const float maxDistortionCorrection = 5f;
166 private const float maxRotationCorrection = 5f;
168 private Bounds defaultBounds =
new Bounds(Vector3.zero, Vector3.one);
169 private MeshFilter backgroundRendererMeshFilter;
170 private Vector3 lastPosition;
171 private Vector3 velocity;
172 private Vector3 distortion;
173 private Vector3 rotation;
174 private Bounds localContentBounds;
175 private Bounds inertialContentBounds;
185 lastPosition = positionTarget.position;
186 inertialContentBounds = defaultBounds;
187 localContentBounds = defaultBounds;
188 velocity = Vector3.zero;
189 backgroundRendererMeshFilter = BackgroundRenderer.GetComponent<MeshFilter>();
195 Vector3 localContentSize = toolTip.LocalContentSize;
196 Vector3 localContentOffset = toolTip.LocalContentOffset;
200 Bounds meshBounds = backgroundRendererMeshFilter.sharedMesh.bounds;
201 localContentSize.x /= meshBounds.size.x;
202 localContentSize.y /= meshBounds.size.y;
203 localContentSize.z = 1;
205 localContentBounds.size = Vector3.one;
206 localContentBounds.center = localContentOffset;
209 private void Update()
212 Vector3 currentPosition = positionTarget.position;
213 velocity = Vector3.Lerp(velocity, lastPosition - currentPosition, 1f / blobInertia * Time.deltaTime);
214 Vector3 currentDistortion = -velocity * blobDistortion;
215 distortion = Vector3.Lerp(distortion, currentDistortion, 1f / blobDistortion * Time.deltaTime);
217 inertialContentBounds.center = inertialContentBounds.center + velocity;
218 Vector3 size = inertialContentBounds.size + distortion;
219 inertialContentBounds.size = size;
221 Vector3 currentRotation = Vector3.zero;
222 currentRotation.x = velocity.x * 360;
223 currentRotation.z = velocity.z * 360;
224 currentRotation.y = velocity.y * 360;
225 currentRotation = rotationTarget.TransformDirection(currentRotation);
226 rotation = Vector3.Lerp (rotation, currentRotation, 1f / blobRotation * Time.deltaTime);
229 inertialContentBounds.center = Vector3.Lerp(inertialContentBounds.center, localContentBounds.center, Time.deltaTime * positionCorrectionStrength);
230 inertialContentBounds.size = Vector3.Lerp(inertialContentBounds.size, localContentBounds.size, Time.deltaTime * distortionCorrectionStrength);
231 rotationTarget.localRotation = Quaternion.Lerp(positionTarget.localRotation, Quaternion.identity, Time.deltaTime * rotationCorrectionStrength);
234 positionTarget.localPosition = inertialContentBounds.center + blobOffset;
235 distortionTarget.localScale = inertialContentBounds.size;
236 rotationTarget.Rotate(velocity * blobRotation * 360);
239 toolTip.AttachPointPosition = attachPointOffset.position;
241 lastPosition = currentPosition;