summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit/UnitRootMotion.cs
blob: 8c6edaa04bdb5c3055de7cefc5d00e87287bd02e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

// 同步root motion到角色根节点
[DisallowMultipleComponent]
public class UnitRootMotion : UnitComponent
{
	RootMotionData m_RootMotionData;

	Dictionary<UnitAnimation.EAnimState, RootMotionData> m_RootMotionDic = new Dictionary<UnitAnimation.EAnimState, RootMotionData>();

	private float m_PrevNormalTime;

	public override void Initialize()
	{
		base.Initialize();
	}

#if false // 用自定义root motion 
    
	public void Reset()
	{
		m_PrevNormalTime = 0;
	}

	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()
	{
		// 因为Unti被旋转了90度,所以这里的deltaPosition的forward是x方向了
		Vector3 dest = m_Owner.unitAnimation.animator.deltaPosition;
		dest.z = 0;

        var state = m_Owner.unitAnimation.layers[0].stateInfo;
        if(state.IsTag("IgnoreY"))
        {
            dest.y = 0;
        }

		//m_Owner.transform.position += RootMotionUtility.ExchangeXZ(dest); // 不需要exchangeXZ
		m_Owner.transform.position += dest;
	}

#endif

}