6 using System.Collections.Generic;
17 #region Public accessors 18 public Vector3 HeadVelocity {
get {
return headVelocity; } }
19 public Vector3 MoveDirection {
get {
return headMoveDirection; } }
21 [Tooltip(
"Minimum velicoty threshold")]
22 public float headVelIdleThresh = 0.5f;
23 [Tooltip(
"Maximum velicoty threshold")]
24 public float headVelMoveThresh = 2f;
27 #region Private members 28 private Vector3 headVelocity;
29 private Vector3 lastHeadPos;
30 private Vector3 newHeadMoveDirection;
31 private Vector3 headMoveDirection = Vector3.one;
34 private bool debugDrawHeadVelocity =
true;
36 private bool debugDrawHeadDirection =
true;
39 private void FixedUpdate()
43 Vector3 headDelta = newHeadPos - lastHeadPos;
45 float moveThreshold = 0.01f;
46 if (headDelta.sqrMagnitude < moveThreshold * moveThreshold)
48 headDelta = Vector3.zero;
51 if (Time.fixedDeltaTime > 0)
53 float velAdjustRate = 3f * Time.fixedDeltaTime;
54 headVelocity = headVelocity * (1f - velAdjustRate) + headDelta * velAdjustRate / Time.fixedDeltaTime;
56 float velThreshold = .1f;
57 if (headVelocity.sqrMagnitude < velThreshold * velThreshold)
59 headVelocity = Vector3.zero;
64 float velP = Mathf.Clamp01(Mathf.InverseLerp(headVelIdleThresh, headVelMoveThresh, headVelocity.magnitude));
66 newHeadMoveDirection = Vector3.Lerp(newHeadPos, headVelocity, velP).normalized;
67 lastHeadPos = newHeadPos;
68 float dirAdjustRate = Mathf.Clamp01(5f * Time.fixedDeltaTime);
70 headMoveDirection = Vector3.Slerp(headMoveDirection, newHeadMoveDirection, dirAdjustRate);
72 if(debugDrawHeadDirection)
74 Debug.DrawLine(lastHeadPos, lastHeadPos + headMoveDirection * 10f, Color.Lerp(Color.red, Color.green, velP));
77 if(debugDrawHeadVelocity)
79 Debug.DrawLine(lastHeadPos, lastHeadPos + headVelocity, Color.yellow);