/// <summary> /// Computes a new local scale for this object /// </summary> protected virtual void ComputeNewLocalScale() { if (_squashing) { float elapsed = MMMaths.Remap(TimescaleTime - _squashStartedAt, 0f, _squashDuration, 0f, 1f); _newLocalScale.x = _initialScale.x + SquashCurve.Evaluate(elapsed) * _squashIntensity; _newLocalScale.y = _initialScale.y - SquashCurve.Evaluate(elapsed) * _squashIntensity; _newLocalScale.z = _initialScale.z + SquashCurve.Evaluate(elapsed) * _squashIntensity; if (elapsed >= 1f) { _squashing = false; } } else { _newLocalScale.x = Mathf.Clamp01(1f / (RemappedVelocity + 0.001f)); _newLocalScale.y = RemappedVelocity; _newLocalScale.z = Mathf.Clamp01(1f / (RemappedVelocity + 0.001f)); _newLocalScale = Vector3.Lerp(Vector3.one, _newLocalScale, VelocityMagnitude * Intensity); } _newLocalScale.x = Mathf.Clamp(_newLocalScale.x, MinimumScale.x, MaximumScale.x); _newLocalScale.y = Mathf.Clamp(_newLocalScale.y, MinimumScale.y, MaximumScale.y); if (Spring) { MMMaths.Spring(ref _springScale, _newLocalScale, ref _springVelocity, SpringDamping, SpringFrequency, SpringSpeed, Time.deltaTime); _newLocalScale = _springScale; } this.transform.localScale = _newLocalScale; }
/// <summary> /// Follows the target, lerping the position or not based on what's been defined in the inspector /// </summary> protected virtual void FollowTargetPosition() { if (Target == null) { return; } if (!FollowPosition) { return; } _newTargetPosition = Target.position + Offset; if (!FollowPositionX) { _newTargetPosition.x = _initialPosition.x; } if (!FollowPositionY) { _newTargetPosition.y = _initialPosition.y; } if (!FollowPositionZ) { _newTargetPosition.z = _initialPosition.z; } float trueDistance = 0f; _direction = (_newTargetPosition - this.transform.position).normalized; trueDistance = Vector3.Distance(this.transform.position, _newTargetPosition); float interpolatedDistance = trueDistance; if (InterpolatePosition) { switch (FollowPositionMode) { case FollowModes.MMLerp: interpolatedDistance = MMMaths.Lerp(0f, trueDistance, FollowPositionSpeed, Time.deltaTime); interpolatedDistance = ApplyMinMaxDistancing(trueDistance, interpolatedDistance); this.transform.Translate(_direction * interpolatedDistance, Space.World); break; case FollowModes.RegularLerp: interpolatedDistance = Mathf.Lerp(0f, trueDistance, Time.deltaTime * FollowPositionSpeed); interpolatedDistance = ApplyMinMaxDistancing(trueDistance, interpolatedDistance); this.transform.Translate(_direction * interpolatedDistance, Space.World); break; case FollowModes.MMSpring: _newPosition = this.transform.position; MMMaths.Spring(ref _newPosition, _newTargetPosition, ref _velocity, PositionSpringDamping, PositionSpringFrequency, FollowPositionSpeed, Time.deltaTime); if (_localSpace) { this.transform.localPosition = _newPosition; } else { this.transform.position = _newPosition; } break; } } else { interpolatedDistance = ApplyMinMaxDistancing(trueDistance, interpolatedDistance); this.transform.Translate(_direction * interpolatedDistance, Space.World); } if (AnchorToInitialPosition) { if (Vector3.Distance(this.transform.position, _initialPosition) > MaxDistanceToAnchor) { if (_localSpace) { this.transform.localPosition = _initialPosition + Vector3.ClampMagnitude(this.transform.localPosition - _initialPosition, MaxDistanceToAnchor); } else { this.transform.position = _initialPosition + Vector3.ClampMagnitude(this.transform.position - _initialPosition, MaxDistanceToAnchor); } } } }