summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/MecanimBridge2D.cs
blob: 809969c011bd03b1ab159e445918ee15fb54f7d8 (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
90
using UnityEngine;

namespace Pathfinding.Examples {
	/// <summary>
	/// Helper for adding animation to agents.
	///
	/// This script will forward the movement velocity to the animator component using the following animator parameters:
	///
	/// - InputMagnitude: Movement speed, normalized by the agent's natural speed. 1 if the agent is moving at its natural speed, and 0 if it is standing still.
	/// - X: Horizontal movement speed, normalized by the agent's natural speed.
	/// - Y: Vertical movement speed, normalized by the agent's natural speed.
	/// </summary>
	[HelpURL("https://arongranberg.com/astar/documentation/stable/mecanimbridge2d.html")]
	public class MecanimBridge2D : VersionedMonoBehaviour {
		/// <summary>
		/// How much to smooth the velocity of the agent.
		///
		/// The velocity will be smoothed out over approximately this number of seconds.
		/// A value of zero indicates no smoothing.
		/// </summary>
		public float velocitySmoothing = 1;

		/// <summary>
		/// The natural movement speed is the speed that the animations are designed for.
		///
		/// One can for example configure the animator to speed up the animation if the agent moves faster than this, or slow it down if the agent moves slower than this.
		/// </summary>
		public float naturalSpeed = 5;

		/// <summary>
		/// How the agent's rotation is handled.
		///
		/// See: <see cref="RotationMode"/>
		/// </summary>
		public RotationMode rotationMode = RotationMode.Hide;

		public enum RotationMode {
			/// <summary>The agent's transform will rotate towards the movement direction</summary>
			RotateTransform,
			/// <summary>
			/// The agent will not visibly rotate.
			///
			/// This is useful if your animation changes the agent's sprite to show a rotation.
			/// Internally, the agent's rotation property will still return the true rotation of the agent.
			///
			/// This is implemented by setting <see cref="FollowerEntity.updateRotation"/> to false on the agent.
			/// </summary>
			Hide,
		}

		/// <summary>Cached reference to the movement script</summary>
		IAstarAI ai;

		/// <summary>Cached Animator component</summary>
		Animator anim;

		Vector2 smoothedVelocity;

		protected override void Awake () {
			base.Awake();
			ai = GetComponent<IAstarAI>();
			anim = GetComponentInChildren<Animator>();
		}

		void Update () {
			if (ai == null || anim == null) return;

			var updateRotation = rotationMode == RotationMode.RotateTransform;
			// TODO: Expose this property using an interface
			if (ai is AIBase aiBase) aiBase.updateRotation = updateRotation;
			else if (ai is AILerp aiLerp) aiLerp.updateRotation = updateRotation;
#if MODULE_ENTITIES
			else if (ai is FollowerEntity follower) follower.updateRotation = updateRotation;
#endif

			var desiredVelocity = naturalSpeed > 0 ? ai.desiredVelocity / naturalSpeed : ai.desiredVelocity;
			var movementPlane = ai.movementPlane;
			var desiredVelocity2D = (Vector2)movementPlane.ToPlane(desiredVelocity, out var _);
			anim.SetFloat("NormalizedSpeed", ai.reachedEndOfPath || desiredVelocity2D.magnitude < 0.1f ? 0f : desiredVelocity2D.magnitude);

			smoothedVelocity = Vector3.Lerp(smoothedVelocity, desiredVelocity2D, velocitySmoothing > 0 ? Time.deltaTime / velocitySmoothing : 1);
			if (smoothedVelocity.magnitude < 0.4f) {
				smoothedVelocity = smoothedVelocity.normalized * 0.4f;
			}

			anim.SetFloat("X", smoothedVelocity.x);
			anim.SetFloat("Y", smoothedVelocity.y);
		}
	}
}