Alek/smooth anim (#19)

* cleanup of animation sync
* smooth animations
This commit is contained in:
amazingalek 2020-02-18 21:39:18 +01:00 committed by GitHub
parent a1177515b3
commit 0e9bfdd9ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 12 deletions

39
QSB/AnimationSync.cs Normal file
View File

@ -0,0 +1,39 @@
using UnityEngine;
using UnityEngine.Networking;
namespace QSB
{
public class AnimationSync : NetworkBehaviour
{
private Animator _anim;
private NetworkAnimator _netAnim;
private void Awake()
{
_anim = gameObject.AddComponent<Animator>();
_netAnim = gameObject.AddComponent<NetworkAnimator>();
_netAnim.animator = _anim;
}
public void Init(Transform body)
{
var bodyAnim = body.GetComponent<Animator>();
var animMirror = body.gameObject.AddComponent<AnimatorMirror>();
if (isLocalPlayer)
{
animMirror.Init(bodyAnim, _anim);
}
else
{
animMirror.Init(_anim, bodyAnim);
}
for (var i = 0; i < _anim.parameterCount; i++)
{
_netAnim.SetParameterAutoSend(i, true);
}
}
}
}

View File

@ -1,13 +1,20 @@
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace QSB
{
public class AnimatorMirror : MonoBehaviour
{
private const float SmoothTime = 0.02f;
private Animator _from;
private Animator _to;
private bool _isRunning;
private float _smoothVelocity;
private readonly Dictionary<string, float> _floatParams = new Dictionary<string, float>();
public void Init(Animator from, Animator to)
{
_from = from;
@ -20,6 +27,10 @@ namespace QSB
{
_to.runtimeAnimatorController = _from.runtimeAnimatorController;
}
foreach (var param in _from.parameters.Where(p => p.type == AnimatorControllerParameterType.Float))
{
_floatParams.Add(param.name, param.defaultFloat);
}
_isRunning = true;
}
@ -29,13 +40,18 @@ namespace QSB
{
return;
}
SyncParams();
SmoothFloats();
}
private void SyncParams()
{
foreach (var fromParam in _from.parameters)
{
switch (fromParam.type)
{
case AnimatorControllerParameterType.Float:
_to.SetFloat(fromParam.name, _from.GetFloat(fromParam.name));
_floatParams[fromParam.name] = _from.GetFloat(fromParam.name);
break;
case AnimatorControllerParameterType.Int:
_to.SetInteger(fromParam.name, _from.GetInteger(fromParam.name));
@ -47,5 +63,15 @@ namespace QSB
}
}
private void SmoothFloats()
{
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);
}
}
}
}

View File

@ -28,7 +28,6 @@ namespace QSB
{
LocalInstance = this;
_body = player;
_body.gameObject.AddComponent<AnimatorMirror>().Init(_body.GetComponent<Animator>(), GetComponent<Animator>());
}
else
{
@ -36,14 +35,9 @@ namespace QSB
_body.GetComponent<PlayerAnimController>().enabled = false;
_body.Find("player_mesh_noSuit:Traveller_HEA_Player/player_mesh_noSuit:Player_Head").gameObject.layer = 0;
_body.Find("Traveller_Mesh_v01:Traveller_Geo/Traveller_Mesh_v01:PlayerSuit_Helmet").gameObject.layer = 0;
_body.gameObject.AddComponent<AnimatorMirror>().Init(GetComponent<Animator>(), _body.GetComponent<Animator>());
}
var netAnim = GetComponent<NetworkAnimator>();
for (var i = 0; i < GetComponent<Animator>().parameterCount; i++)
{
netAnim.SetParameterAutoSend(i, true);
}
GetComponent<AnimationSync>().Init(_body);
}
private void SetFirstSector()

View File

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

View File

@ -10,9 +10,7 @@ namespace QSB
var assetBundle = QSB.Helper.Assets.LoadBundle("assets/network");
playerPrefab = assetBundle.LoadAsset<GameObject>("assets/networkplayer.prefab");
playerPrefab.AddComponent<NetworkPlayer>();
var anim = playerPrefab.AddComponent<Animator>();
playerPrefab.AddComponent<NetworkAnimator>().animator = anim;
playerPrefab.AddComponent<AnimationSync>();
}
public override void OnStartServer()