Skip to content

Commit 3a2a5f6

Browse files
More Performance Improvements
1 parent 814d1a4 commit 3a2a5f6

File tree

6 files changed

+182
-93
lines changed

6 files changed

+182
-93
lines changed

Assets/Resources/Scenes/SampleScene.unity

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,16 @@ PrefabInstance:
206206
propertyPath: walkSpeed
207207
value: 0
208208
objectReference: {fileID: 0}
209+
- target: {fileID: 3763241649378609582, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
210+
propertyPath: showGizmos
211+
value: 1
212+
objectReference: {fileID: 0}
209213
- target: {fileID: 3763241649378609582, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
210214
propertyPath: pathUpdateTime
211-
value: 0.3
215+
value: 0.2
212216
objectReference: {fileID: 0}
213217
m_RemovedComponents: []
214218
m_SourcePrefab: {fileID: 100100000, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
215-
--- !u!4 &260715481 stripped
216-
Transform:
217-
m_CorrespondingSourceObject: {fileID: 3763241649378609581, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
218-
m_PrefabInstance: {fileID: 2130127189}
219-
m_PrefabAsset: {fileID: 0}
220219
--- !u!1001 &299077549
221220
PrefabInstance:
222221
m_ObjectHideFlags: 0
@@ -300,9 +299,13 @@ PrefabInstance:
300299
propertyPath: walkSpeed
301300
value: 0
302301
objectReference: {fileID: 0}
302+
- target: {fileID: 3763241649378609582, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
303+
propertyPath: showGizmos
304+
value: 1
305+
objectReference: {fileID: 0}
303306
- target: {fileID: 3763241649378609582, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
304307
propertyPath: pathUpdateTime
305-
value: 0.3
308+
value: 0.2
306309
objectReference: {fileID: 0}
307310
m_RemovedComponents: []
308311
m_SourcePrefab: {fileID: 100100000, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
@@ -336,7 +339,7 @@ Transform:
336339
- {fileID: 1008989324}
337340
- {fileID: 891452763}
338341
m_Father: {fileID: 0}
339-
m_RootOrder: 5
342+
m_RootOrder: 4
340343
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
341344
--- !u!1 &885237147
342345
GameObject:
@@ -429,7 +432,7 @@ Transform:
429432
m_LocalScale: {x: 1, y: 1, z: 1}
430433
m_Children: []
431434
m_Father: {fileID: 0}
432-
m_RootOrder: 2
435+
m_RootOrder: 1
433436
m_LocalEulerAnglesHint: {x: 58.042, y: 84.461, z: 98.258}
434437
--- !u!1 &891452759
435438
GameObject:
@@ -459,7 +462,7 @@ CapsuleCollider:
459462
m_GameObject: {fileID: 891452759}
460463
m_Material: {fileID: 0}
461464
m_IsTrigger: 0
462-
m_Enabled: 1
465+
m_Enabled: 0
463466
m_Radius: 0.5
464467
m_Height: 2
465468
m_Direction: 1
@@ -660,12 +663,14 @@ MonoBehaviour:
660663
obsticleMask:
661664
serializedVersion: 2
662665
m_Bits: 128
666+
target: {fileID: 633522798}
663667
gridSize: 100
664668
sampleSize: 50
665669
colliderRadius: 0.5
666670
walkableUpdateTime: 0.5
667-
gridUpdateTime: 1
668-
target: {fileID: 633522798}
671+
gridUpdateTime: 3
672+
maxIncline: 3
673+
nonWalkableNeighborCost: 10
669674
debugView: 0
670675
--- !u!114 &1008989323
671676
MonoBehaviour:
@@ -708,6 +713,7 @@ MonoBehaviour:
708713
m_EditorClassIdentifier:
709714
skipTime: 10
710715
breakTime: 100
716+
minHeightDifference: 0.2
711717
--- !u!1 &1020511618
712718
GameObject:
713719
m_ObjectHideFlags: 0
@@ -793,7 +799,7 @@ Transform:
793799
- {fileID: 1511754981}
794800
- {fileID: 974375503}
795801
m_Father: {fileID: 0}
796-
m_RootOrder: 3
802+
m_RootOrder: 2
797803
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
798804
--- !u!1 &1044695418
799805
GameObject:
@@ -1228,8 +1234,8 @@ Camera:
12281234
near clip plane: 0.3
12291235
far clip plane: 1000
12301236
field of view: 60
1231-
orthographic: 0
1232-
orthographic size: 5
1237+
orthographic: 1
1238+
orthographic size: 97.25
12331239
m_Depth: 0
12341240
m_CullingMask:
12351241
serializedVersion: 2
@@ -1253,7 +1259,7 @@ Transform:
12531259
m_PrefabAsset: {fileID: 0}
12541260
m_GameObject: {fileID: 1268584959}
12551261
m_LocalRotation: {x: 0.7071067, y: 0.00000017881392, z: -0.000000111758695, w: 0.7071069}
1256-
m_LocalPosition: {x: 98.43274, y: 110.61264, z: 97.45744}
1262+
m_LocalPosition: {x: 100, y: 110.61264, z: 100}
12571263
m_LocalScale: {x: 1, y: 1, z: 1}
12581264
m_Children: []
12591265
m_Father: {fileID: 0}
@@ -1266,12 +1272,12 @@ MonoBehaviour:
12661272
m_PrefabInstance: {fileID: 0}
12671273
m_PrefabAsset: {fileID: 0}
12681274
m_GameObject: {fileID: 1268584959}
1269-
m_Enabled: 0
1275+
m_Enabled: 1
12701276
m_EditorHideFlags: 0
12711277
m_Script: {fileID: 11500000, guid: 737b0ceeeabc66e4d9e4e033b3d65f7f, type: 3}
12721278
m_Name:
12731279
m_EditorClassIdentifier:
1274-
target: {fileID: 260715481}
1280+
target: {fileID: 633522798}
12751281
--- !u!1 &1368896038
12761282
GameObject:
12771283
m_ObjectHideFlags: 0
@@ -1545,9 +1551,13 @@ PrefabInstance:
15451551
propertyPath: walkSpeed
15461552
value: 0
15471553
objectReference: {fileID: 0}
1554+
- target: {fileID: 3763241649378609582, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
1555+
propertyPath: showGizmos
1556+
value: 1
1557+
objectReference: {fileID: 0}
15481558
- target: {fileID: 3763241649378609582, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
15491559
propertyPath: pathUpdateTime
1550-
value: 0.3
1560+
value: 0.2
15511561
objectReference: {fileID: 0}
15521562
m_RemovedComponents: []
15531563
m_SourcePrefab: {fileID: 100100000, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
@@ -1919,9 +1929,13 @@ PrefabInstance:
19191929
propertyPath: walkSpeed
19201930
value: 0
19211931
objectReference: {fileID: 0}
1932+
- target: {fileID: 3763241649378609582, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
1933+
propertyPath: showGizmos
1934+
value: 1
1935+
objectReference: {fileID: 0}
19221936
- target: {fileID: 3763241649378609582, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
19231937
propertyPath: pathUpdateTime
1924-
value: 0.3
1938+
value: 0.2
19251939
objectReference: {fileID: 0}
19261940
m_RemovedComponents: []
19271941
m_SourcePrefab: {fileID: 100100000, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
@@ -2008,9 +2022,13 @@ PrefabInstance:
20082022
propertyPath: walkSpeed
20092023
value: 0
20102024
objectReference: {fileID: 0}
2025+
- target: {fileID: 3763241649378609582, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
2026+
propertyPath: showGizmos
2027+
value: 1
2028+
objectReference: {fileID: 0}
20112029
- target: {fileID: 3763241649378609582, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
20122030
propertyPath: pathUpdateTime
2013-
value: 0.3
2031+
value: 0.2
20142032
objectReference: {fileID: 0}
20152033
m_RemovedComponents: []
20162034
m_SourcePrefab: {fileID: 100100000, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
@@ -2107,7 +2125,7 @@ Transform:
21072125
m_LocalScale: {x: 13.366748, y: 7.292145, z: 37.61142}
21082126
m_Children: []
21092127
m_Father: {fileID: 0}
2110-
m_RootOrder: 4
2128+
m_RootOrder: 3
21112129
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
21122130
--- !u!1 &2083702460
21132131
GameObject:
@@ -2221,7 +2239,7 @@ PrefabInstance:
22212239
objectReference: {fileID: 0}
22222240
- target: {fileID: 3763241649378609581, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
22232241
propertyPath: m_RootOrder
2224-
value: 1
2242+
value: 5
22252243
objectReference: {fileID: 0}
22262244
- target: {fileID: 3763241649378609581, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
22272245
propertyPath: m_LocalScale.x
@@ -2287,5 +2305,13 @@ PrefabInstance:
22872305
propertyPath: walkSpeed
22882306
value: 0
22892307
objectReference: {fileID: 0}
2308+
- target: {fileID: 3763241649378609582, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
2309+
propertyPath: showGizmos
2310+
value: 1
2311+
objectReference: {fileID: 0}
2312+
- target: {fileID: 3763241649378609582, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}
2313+
propertyPath: pathUpdateTime
2314+
value: 0.2
2315+
objectReference: {fileID: 0}
22902316
m_RemovedComponents: []
22912317
m_SourcePrefab: {fileID: 100100000, guid: 85971c17debd7f74ea3a1f5d5c34f488, type: 3}

Assets/Resources/Scripts/Agent.cs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using UnityEngine;
2-
using System.Collections.Generic;
32

43
public class Agent : MonoBehaviour
54
{
6-
public AstarGrid astar;
5+
public AstarGrid astarGrid;
76
public new Rigidbody rigidbody;
87
public Transform rotationHost;
9-
public float maxGridDistance = 3;
8+
public bool showGizmos = false;
109
[Header("Follow Settings")]
1110
public Transform target;
11+
public float maxGridDistance = 3;
1212
public float pathUpdateTime = 0.5f;
1313
public bool targetPlayer = false;
1414
public float maxIncline = 2;
@@ -29,11 +29,21 @@ public class Agent : MonoBehaviour
2929
public string runAnimationName = "Run";
3030
public string attackAnimationName = "Attack";
3131

32-
// Private
32+
[Header("Output")]
33+
public float noPathPenaltySeconds = 1f;
3334
public bool isRunning = false;
35+
public float stuckTimer = 0;
3436
public Vector3[] path;
37+
38+
// Private
3539
float speed = 0;
3640
Node closestNode;
41+
Vector3 stuckPosition = Vector3.zero;
42+
Vector3 direction;
43+
Vector3 lookDirection;
44+
45+
// Timers
46+
float timer = 0;
3747

3848
public enum States
3949
{
@@ -46,50 +56,51 @@ public enum States
4656

4757
private void Start()
4858
{
49-
if (!astar)
50-
astar = AstarGrid.Instance;
59+
if (!astarGrid)
60+
astarGrid = AstarGrid.Instance;
5161
if (targetPlayer)
52-
target = astar.target;
62+
target = astarGrid.target;
5363
}
5464

55-
public void OnPathFound(Vector3[] newPath, bool success)
65+
bool isUpdatingPath = false;
66+
public void OnPathResult(Vector3[] newPath, bool success)
5667
{
68+
isUpdatingPath = false;
5769
if (success)
5870
{
5971
path = newPath;
6072
}
6173
else
6274
{
6375
path = null;
64-
timer += 2.5f;
76+
timer += noPathPenaltySeconds;
6577
}
6678
}
6779

6880
//*
69-
float timer = 0;
70-
public float stuckTimer = 0;
71-
Vector3 stuckPosition = Vector3.zero;
72-
Vector3 direction;
73-
Vector3 lookDirection;
7481
private void Update()
7582
{
7683
if (!target) return;
7784

7885
speed = 0;
7986

80-
timer -= Time.deltaTime;
87+
if (!isUpdatingPath)
88+
timer -= Time.deltaTime;
8189
if (timer <= 0)
8290
{
8391
timer = pathUpdateTime;
84-
if (AstarGrid.Instance.nodeGrid != null)
85-
PathRequestManager.RequestPath(new PathRequest(AstarGrid.Instance.nodeGrid, transform.position, target.position, OnPathFound));
92+
if (astarGrid.nodeGrid != null)
93+
{
94+
isUpdatingPath = true;
95+
PathRequestManager.RequestPath(new PathRequest(astarGrid, transform.position, target.position, OnPathResult));
96+
}
8697
}
8798

88-
if (Vector3.Distance(transform.position, astar.transform.position) < astar.gridSize + astar.gridSize / 2) // if we're not to far from the grid
99+
if (Vector3.Distance(transform.position, astarGrid.transform.position) < astarGrid.gridSize) // if we're not too far from the grid
89100
{
90101
if (closestNode == null || Vector3.Distance(closestNode.position, transform.position) > maxGridDistance) // and we're too far from the closest node
91102
{
92-
closestNode = AstarGrid.Instance.NodeFromWorldPoint(transform.position);
103+
closestNode = astarGrid.NodeFromWorldPoint(transform.position);
93104

94105
if (closestNode == null)
95106
{
@@ -122,10 +133,7 @@ private void Update()
122133
if (Vector3.Distance(transform.position, target.position) >= stopDistance)
123134
{
124135
direction = path[0] - transform.position;
125-
if (path.Length > 4)
126-
lookDirection = path[0] - transform.position;
127-
else if (target)
128-
lookDirection = target.position - transform.position;
136+
lookDirection = path[0] - transform.position;
129137
}
130138
else
131139
speed = 0;
@@ -206,12 +214,12 @@ private void FixedUpdate()
206214

207215
public void OnDrawGizmos()
208216
{
209-
if (path != null)
217+
if (path != null && showGizmos)
210218
{
211219
for (int i = 1; i < path.Length; i++)
212220
{
213221
Gizmos.color = Color.black;
214-
Gizmos.DrawCube(path[i], Vector3.one);
222+
Gizmos.DrawCube(path[i], Vector3.one * 0.55f);
215223

216224
if (i < 2)
217225
Gizmos.DrawLine(transform.position, path[1]);

0 commit comments

Comments
 (0)