Skip to content

Commit dfd091c

Browse files
committed
12 Move Enemy On Path
1 parent 84d72da commit dfd091c

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

Laser Defender/Assets/Prefabs/Enemy.prefab

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ MonoBehaviour:
5656
- {fileID: 4661226994601206, guid: 513e949a475d76149ac4cb3540576af0, type: 2}
5757
- {fileID: 4061757090832336, guid: 513e949a475d76149ac4cb3540576af0, type: 2}
5858
- {fileID: 4119412733113868, guid: 513e949a475d76149ac4cb3540576af0, type: 2}
59+
moveSpeed: 2
5960
--- !u!212 &212918273063680576
6061
SpriteRenderer:
6162
m_ObjectHideFlags: 1

Laser Defender/Assets/Scenes/Game.unity

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,21 @@ Prefab:
152152
propertyPath: m_RootOrder
153153
value: 7
154154
objectReference: {fileID: 0}
155+
- target: {fileID: 1818158268515574, guid: 513e949a475d76149ac4cb3540576af0, type: 2}
156+
propertyPath: m_Icon
157+
value:
158+
objectReference: {fileID: 1206586993520771344, guid: 0000000000000000d000000000000000,
159+
type: 0}
160+
- target: {fileID: 1716279003543564, guid: 513e949a475d76149ac4cb3540576af0, type: 2}
161+
propertyPath: m_Icon
162+
value:
163+
objectReference: {fileID: 1206586993520771344, guid: 0000000000000000d000000000000000,
164+
type: 0}
165+
- target: {fileID: 1220463475794502, guid: 513e949a475d76149ac4cb3540576af0, type: 2}
166+
propertyPath: m_Icon
167+
value:
168+
objectReference: {fileID: 1206586993520771344, guid: 0000000000000000d000000000000000,
169+
type: 0}
155170
m_RemovedComponents: []
156171
m_SourcePrefab: {fileID: 100100000, guid: 513e949a475d76149ac4cb3540576af0, type: 2}
157172
m_IsPrefabAsset: 0
@@ -164,11 +179,11 @@ Prefab:
164179
m_Modifications:
165180
- target: {fileID: 4068133663285090, guid: 6e491cad0c322694c81cc5833ef869d5, type: 2}
166181
propertyPath: m_LocalPosition.x
167-
value: 0
182+
value: 3.87
168183
objectReference: {fileID: 0}
169184
- target: {fileID: 4068133663285090, guid: 6e491cad0c322694c81cc5833ef869d5, type: 2}
170185
propertyPath: m_LocalPosition.y
171-
value: 4.41
186+
value: 5.14
172187
objectReference: {fileID: 0}
173188
- target: {fileID: 4068133663285090, guid: 6e491cad0c322694c81cc5833ef869d5, type: 2}
174189
propertyPath: m_LocalPosition.z
@@ -194,6 +209,11 @@ Prefab:
194209
propertyPath: m_RootOrder
195210
value: 5
196211
objectReference: {fileID: 0}
212+
- target: {fileID: 114331410767387138, guid: 6e491cad0c322694c81cc5833ef869d5,
213+
type: 2}
214+
propertyPath: moveSpeed
215+
value: 5
216+
objectReference: {fileID: 0}
197217
m_RemovedComponents: []
198218
m_SourcePrefab: {fileID: 100100000, guid: 6e491cad0c322694c81cc5833ef869d5, type: 2}
199219
m_IsPrefabAsset: 0

Laser Defender/Assets/Scripts/EnemyPathing.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,37 @@
55
public class EnemyPathing : MonoBehaviour {
66

77
[SerializeField] List<Transform> waypoints;
8+
[SerializeField] float moveSpeed = 2f;
9+
int waypointIndex = 0;
810

911
// Use this for initialization
1012
void Start () {
11-
13+
transform.position = waypoints[waypointIndex].transform.position;
1214
}
1315

1416
// Update is called once per frame
15-
void Update () {
16-
17-
}
17+
void Update ()
18+
{
19+
Move();
20+
}
21+
22+
private void Move()
23+
{
24+
if (waypointIndex <= waypoints.Count - 1)
25+
{
26+
var targetPosition = waypoints[waypointIndex].transform.position;
27+
var movementThisFrame = moveSpeed * Time.deltaTime;
28+
transform.position = Vector2.MoveTowards
29+
(transform.position, targetPosition, movementThisFrame);
30+
31+
if (transform.position == targetPosition)
32+
{
33+
waypointIndex++;
34+
}
35+
}
36+
else
37+
{
38+
Destroy(gameObject);
39+
}
40+
}
1841
}

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,15 @@ Able to destroy GameObjects which collide with our trigger volume.
140140
3. Store our path waypoints in our list.
141141

142142
**After watching (learning outcomes)…**
143-
Create a list to store enemy path waypoints.
143+
Create a list to store enemy path waypoints.
144+
145+
146+
### 12 Move Enemy On Path ###
147+
148+
**In this video (objectives)…**
149+
150+
1. Write pseudocode for our enemy movement.
151+
2. Use the MoveTowards() method to move the enemy.
152+
153+
**After watching (learning outcomes)…**
154+
Move an enemy GameObject along a path using waypoints.

0 commit comments

Comments
 (0)