diff options
author | chai <chaifix@163.com> | 2021-08-02 08:35:26 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-08-02 08:35:26 +0800 |
commit | ce73a13f28e5a947df8f1f87f1f1be20010952ec (patch) | |
tree | 2c430bcd93f2c09bd35340def7c556c7294d6b5d /Assets/Scripts/Unit/UnitRootMotion.cs | |
parent | 81acfeb9a1d88075899d9c21064330915caa59a4 (diff) |
* 测试
Diffstat (limited to 'Assets/Scripts/Unit/UnitRootMotion.cs')
-rw-r--r-- | Assets/Scripts/Unit/UnitRootMotion.cs | 81 |
1 files changed, 68 insertions, 13 deletions
diff --git a/Assets/Scripts/Unit/UnitRootMotion.cs b/Assets/Scripts/Unit/UnitRootMotion.cs index a2607700..18e0093f 100644 --- a/Assets/Scripts/Unit/UnitRootMotion.cs +++ b/Assets/Scripts/Unit/UnitRootMotion.cs @@ -1,24 +1,79 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
+#if UNITY_EDITOR
+using UnityEditor;
+#endif
// 同步root motion到角色根节点
[DisallowMultipleComponent]
-public class UnitRootMotion : MonoBehaviour
+public class UnitRootMotion : UnitComponent
{
- Transform m_Root;
- Animator m_Animator; - - private void Awake()
- {
- m_Root = transform.parent;
- m_Animator = GetComponent<Animator>();
- }
-
- public Vector3 UpdateRootMotion()
+ RootMotionData m_RootMotionData;
+
+ Dictionary<UnitAnimation.EAnimState, RootMotionData> m_RootMotionDic = new Dictionary<UnitAnimation.EAnimState, RootMotionData>();
+
+ private float m_PrevNormalTime;
+
+ public override void Initialize()
{
-
- return Vector3.zero;
+ base.Initialize();
+ }
+
+ public void Reset()
+ {
+ m_PrevNormalTime = 0;
+ }
+
+#if false // 用自定义root motion
+ public override void OnUpdate()
+ {
+ base.OnUpdate();
+
+ var state = m_Owner.unitAnimation.curState;
+ float playbackTime = m_Owner.unitAnimation.playbackTime;
+
+ var rootMotion = m_RootMotionDic[state];
+ float normalTime = (playbackTime % rootMotion.animationLength) / rootMotion.animationLength;
+
+ if (m_PrevNormalTime > normalTime)
+ m_PrevNormalTime = 0;
+
+ m_Owner.transform.position += rootMotion.GetRootMotionDistance(m_PrevNormalTime, normalTime);
+ m_PrevNormalTime = normalTime;
}
+ public void SetUpRootMotion(string unitFolder, UnitActionData actions)
+ {
+ if (actions == null)
+ return;
+
+ foreach (var action in actions.actions)
+ {
+#if UNITY_EDITOR
+ AnimationClip clip = action.Value;
+ string name = clip.name;
+ string path = unitFolder + "RootMotion/" + name + ".asset";
+ RootMotionData data = AssetDatabase.LoadAssetAtPath<RootMotionData>(path);
+ m_RootMotionDic.Add(action.Key, data);
+#endif
+ }
+ }
+
+#else
+
+ public override void OnUpdate()
+ {
+ base.OnUpdate();
+ }
+
+ public void UpdateRootMotion()
+ {
+ Vector3 dest = m_Owner.unitAnimation.animator.deltaPosition;
+ dest.x = 0;
+ m_Owner.transform.position += dest;
+ }
+
+#endif
+
}
|