Skip to content

Commit

Permalink
Replaced nullable value types with Start/OnCustomStart initialization…
Browse files Browse the repository at this point in the history
…s in Custom and Hidden Agent respectively
  • Loading branch information
jadvrodrigues committed Dec 14, 2020
1 parent d26d89b commit e00c9ae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 78 deletions.
79 changes: 20 additions & 59 deletions Assets/CustomNavMesh/Scripts/CustomNavMeshAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
[DisallowMultipleComponent]
public class CustomNavMeshAgent : CustomMonoBehaviour
{
Transform savedParent;
Vector3 savedPosition;
Vector3 savedRotation;
Vector3 savedScale;

/// <summary>
/// A delegate which can be used to register callback methods to be invoked after the agent is changed.
/// </summary>
Expand Down Expand Up @@ -333,59 +338,6 @@ NavMeshAgent NavMeshAgent
}
}

Transform savedParent;

Vector3? savedPosition;
Vector3? SavedPosition
{
get
{
if (!savedPosition.HasValue)
{
savedPosition = transform.localPosition;
}
return savedPosition;
}
set
{
savedPosition = value;
}
}

Vector3? savedRotation;
Vector3? SavedRotation
{
get
{
if (!savedRotation.HasValue)
{
savedRotation = transform.localRotation.eulerAngles;
}
return savedRotation;
}
set
{
savedRotation = value;
}
}

Vector3? savedScale;
Vector3? SavedScale
{
get
{
if (!savedScale.HasValue)
{
savedScale = transform.localScale;
}
return savedScale;
}
set
{
savedScale = value;
}
}

/// <summary>
/// Apply relative movement to current position.
/// If the agent has a path it will be adjusted.
Expand Down Expand Up @@ -458,15 +410,24 @@ public void RecordNavMeshAgent()
#endif
}

// hide Start method because this has to be triggered both inside and outside
// of Play mode and the inherited OnCustomStart is only called in Play mode
new void Start()
{
savedPosition = transform.localPosition;
savedRotation = transform.localRotation.eulerAngles;
savedScale = transform.localScale;
}

// hide Update method because this has to be triggered both inside and outside
// of Play mode and the inherited OnCustomUpdate is only called in Play mode
new void Update()
{
if (transform.hasChanged)
{
if (SavedPosition != transform.localPosition)
if (savedPosition != transform.localPosition)
{
SavedPosition = transform.localPosition;
savedPosition = transform.localPosition;
onPositionChange?.Invoke();
}

Expand All @@ -476,15 +437,15 @@ public void RecordNavMeshAgent()
onParentChange?.Invoke();
}

if (SavedRotation != transform.localRotation.eulerAngles)
if (savedRotation != transform.localRotation.eulerAngles)
{
SavedRotation = transform.localRotation.eulerAngles;
savedRotation = transform.localRotation.eulerAngles;
onRotationChange?.Invoke();
}

if (SavedScale != transform.localScale)
if (savedScale != transform.localScale)
{
SavedScale = transform.localScale;
savedScale = transform.localScale;
onScaleChange?.Invoke();
}

Expand Down
27 changes: 8 additions & 19 deletions Assets/CustomNavMesh/Scripts/HiddenNavMeshAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class HiddenNavMeshAgent : CustomMonoBehaviour
const float destSamplingModifier = 6.0f;

bool subscribed; // used to avoid subscribing twice
Vector3 lastPosition;

Vector3? destination;
float timer; // count the time since agent changed mode or block mode is refreshed
Expand Down Expand Up @@ -138,23 +139,6 @@ bool IsBlocking
}
}

Vector3? lastPosition;
Vector3? LastPosition
{
get
{
if(!lastPosition.HasValue)
{
lastPosition = transform.position;
}
return lastPosition;
}
set
{
lastPosition = value;
}
}

float surfaceAgentRadius = 0.0f;
float SurfaceAgentRadius
{
Expand Down Expand Up @@ -264,6 +248,11 @@ protected override void OnCustomDisable()
TrySubscribe();
}

protected override void OnCustomStart()
{
lastPosition = transform.position;
}

protected override void OnCustomUpdate()
{
Vector3 agentPos = CustomAgent.transform.position;
Expand All @@ -275,7 +264,7 @@ protected override void OnCustomUpdate()
transform.position.y,
agentPos.z + translation.z);

float currentSpeed = Vector3.Distance(transform.position, LastPosition.Value) / Time.deltaTime;
float currentSpeed = Vector3.Distance(transform.position, lastPosition) / Time.deltaTime;

if(currentSpeed < CustomAgent.UnblockSpeedThreshold) // if it did not surpass the speed threshold
{
Expand Down Expand Up @@ -307,7 +296,7 @@ protected override void OnCustomUpdate()
}
}

LastPosition = transform.position;
lastPosition = transform.position;
}

void SwitchToAgent()
Expand Down

0 comments on commit e00c9ae

Please sign in to comment.