protected virtual void OnEnable() { velocitySamples = new Vector3[VelocityAverageFrames]; angularVelocitySamples = new Vector3[AngularVelocityAverageFrames]; previousPosition = Source.TryGetPosition(); previousRotation = Source.TryGetRotation(); previousRelativePosition = RelativeTo.TryGetPosition(); previousRelativeRotation = RelativeTo.TryGetRotation(); }
/// <summary> /// Collects samples for velocity. /// </summary> /// <param name="factor">The multiplier to apply to the transform difference.</param> protected virtual void EstimateVelocity(float factor) { if (velocitySamples.Length == 0) { return; } Vector3 currentRelativePosition = RelativeTo.TryGetPosition(); Vector3 relativeDeltaPosition = currentRelativePosition - previousRelativePosition; Vector3 currentPosition = Source.TryGetPosition(); int sampleIndex = currentSampleCount % velocitySamples.Length; velocitySamples[sampleIndex] = factor * (currentPosition - previousPosition - relativeDeltaPosition); previousPosition = currentPosition; previousRelativePosition = currentRelativePosition; }
protected virtual void OnAfterRelativeToChange() { previousRelativePosition = RelativeTo.TryGetPosition(); previousRelativeRotation = RelativeTo.TryGetRotation(); }