Alek/smooth anim fix (#21)

* fixed smoothing velocity
This commit is contained in:
amazingalek 2020-02-19 12:57:16 +01:00 committed by GitHub
parent 0e9bfdd9ae
commit dcb36a2820
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 8 deletions

View File

@ -6,14 +6,13 @@ namespace QSB
{
public class AnimatorMirror : MonoBehaviour
{
private const float SmoothTime = 0.02f;
private const float SmoothTime = 0.05f;
private Animator _from;
private Animator _to;
private bool _isRunning;
private float _smoothVelocity;
private readonly Dictionary<string, float> _floatParams = new Dictionary<string, float>();
private readonly Dictionary<string, FloatAnimParam> _floatParams = new Dictionary<string, FloatAnimParam>();
public void Init(Animator from, Animator to)
{
@ -29,7 +28,7 @@ namespace QSB
}
foreach (var param in _from.parameters.Where(p => p.type == AnimatorControllerParameterType.Float))
{
_floatParams.Add(param.name, param.defaultFloat);
_floatParams.Add(param.name, new FloatAnimParam());
}
_isRunning = true;
}
@ -51,7 +50,7 @@ namespace QSB
switch (fromParam.type)
{
case AnimatorControllerParameterType.Float:
_floatParams[fromParam.name] = _from.GetFloat(fromParam.name);
_floatParams[fromParam.name].Target = _from.GetFloat(fromParam.name);
break;
case AnimatorControllerParameterType.Int:
_to.SetInteger(fromParam.name, _from.GetInteger(fromParam.name));
@ -67,9 +66,8 @@ namespace QSB
{
foreach (var floatParam in _floatParams)
{
var current = _to.GetFloat(floatParam.Key);
var value = Mathf.SmoothDamp(current, floatParam.Value, ref _smoothVelocity, SmoothTime);
_to.SetFloat(floatParam.Key, value);
var current = floatParam.Value.Smooth(SmoothTime);
_to.SetFloat(floatParam.Key, current);
}
}

19
QSB/FloatAnimParam.cs Normal file
View File

@ -0,0 +1,19 @@
using UnityEngine;
namespace QSB
{
public class FloatAnimParam
{
public float Current { get; private set; }
public float Target { get; set; }
private float _velocity;
public float Smooth(float smoothTime)
{
Current = Mathf.SmoothDamp(Current, Target, ref _velocity, smoothTime);
return Current;
}
}
}

View File

@ -92,6 +92,7 @@
<ItemGroup>
<Compile Include="AnimationSync.cs" />
<Compile Include="AnimatorMirror.cs" />
<Compile Include="FloatAnimParam.cs" />
<Compile Include="DebugLog.cs" />
<Compile Include="QSBBehaviour.cs" />
<Compile Include="PreserveTimeScale.cs" />