Skip to content

Commit 8bfdcb2

Browse files
committed
Merge branch 'add-monsters'
2 parents 79d9b95 + 5251080 commit 8bfdcb2

File tree

7 files changed

+298
-57
lines changed

7 files changed

+298
-57
lines changed

Assets/MonsterController.cs

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class MonsterController : MonoBehaviour
6+
{
7+
// The character the monsters will follow
8+
public Transform leader;
9+
// List of monsters following the leader
10+
public List<Transform> monsters = new List<Transform>();
11+
// Maximum number of monsters
12+
public int maxMonsters = 10;
13+
// Minimum distance between monsters
14+
public float minDistance = 0.25f;
15+
// Distance away from waypoint to be considered "arrived"
16+
public float waypointRadius = 1.0f;
17+
// Speed to reach waypoint
18+
public float speed = 1;
19+
// Prefab of monsters being instantiated
20+
public GameObject monsterPrefab;
21+
// GameObject the monsters are children of
22+
public GameObject monsterHolder;
23+
// Rate of time a waypoint gets made
24+
public float waypointRate = 0.1f;
25+
// Max number of waypoints at a time
26+
public float waypointMax = 10;
27+
28+
// DEBUG: Limits spacebar presses to one at a time
29+
private bool spawning = false;
30+
// Distance between current monster and target location
31+
private float dis;
32+
// Time the previous waypoint was created at
33+
private float prevWaypointTime = 0;
34+
// List of waypoints created by the leader
35+
private List<Vector3> waypoints = new List<Vector3>();
36+
// Current monster being moved
37+
private Transform currMonster;
38+
// Previous monster in the chain
39+
private Transform prevMonster;
40+
41+
// Start is called before the first frame update
42+
void Start()
43+
{
44+
Debug.Log("START");
45+
}
46+
47+
// FixedUpdate is called at a regular interval
48+
void FixedUpdate()
49+
{
50+
// Move all the monsters
51+
Move();
52+
53+
// DEBUG: Add monsters with spacebar
54+
if (Input.GetKey(KeyCode.Space))
55+
{
56+
AddMonster();
57+
}
58+
else
59+
{
60+
spawning = false;
61+
}
62+
}
63+
64+
// Moves the first monster to the next waypoint
65+
private void MoveToNextWaypoint()
66+
{
67+
// Defines current monster and speed
68+
float currSpeed = speed;
69+
currMonster = monsters[0];
70+
71+
// Calculates distance between current monster and next waypoint
72+
dis = Vector3.Distance(waypoints[0], currMonster.position);
73+
74+
// If current monster is close enough to waypoint
75+
if (dis <= waypointRadius)
76+
{
77+
// Remove the waypoint from the list and exit out
78+
waypoints.RemoveAt(0);
79+
return;
80+
}
81+
82+
// Gets the position of the waypoint and zeroes the z axis
83+
Vector3 newPos = waypoints[0];
84+
newPos.z = 0;
85+
86+
// Slerps position to waypoint position
87+
float T;
88+
T = Time.deltaTime * dis / waypointRadius * currSpeed;
89+
if (T > 0.5f)
90+
T = 0.5f;
91+
currMonster.position = Vector3.Slerp(currMonster.position, newPos, T);
92+
}
93+
94+
// Moves ALL monsters; called on FixedUpdate
95+
public void Move()
96+
{
97+
// Defines current speed
98+
float currSpeed = speed;
99+
100+
// If enough time has passed since last waypoint and there are less than max waypoints
101+
if (Time.time > prevWaypointTime + waypointRate && waypoints.Count < waypointMax)
102+
{
103+
// Add waypoint and reset timer
104+
waypoints.Add(leader.position);
105+
prevWaypointTime = Time.time;
106+
}
107+
108+
// If a monster and a waypoint exists
109+
if (monsters.Count > 0 && waypoints.Count > 0)
110+
{
111+
// Moves the first monster in the chain to the next waypoint
112+
MoveToNextWaypoint();
113+
}
114+
115+
// For each monster after the first monster in the chain...
116+
for (int i = 1; i < monsters.Count; i++)
117+
{
118+
// Set current monster and previous monster
119+
currMonster = monsters[i];
120+
prevMonster = monsters[i - 1];
121+
122+
// Get distance between current and previous monsters
123+
dis = Vector3.Distance(prevMonster.position, currMonster.position);
124+
125+
// Get position of previous monster with zeroed Z position
126+
Vector3 newPos = prevMonster.position;
127+
newPos.z = monsters[0].position.z;
128+
129+
// Slerps position and rotation to previous monster in chain
130+
float T;
131+
T = Time.deltaTime * dis / minDistance * currSpeed;
132+
if (T > 0.5f)
133+
T = 0.5f;
134+
currMonster.position = Vector3.Slerp(currMonster.position, newPos, T);
135+
currMonster.rotation = Quaternion.Slerp(currMonster.rotation, prevMonster.rotation, T);
136+
}
137+
}
138+
139+
// Adds a monster to the chain
140+
public void AddMonster()
141+
{
142+
// If monster count is less than max
143+
// DEBUG: and spacebar hasn't been pressed yet
144+
if (monsters.Count < maxMonsters && !spawning)
145+
{
146+
// Instantiate the monster at leader if it's first monster, or previous monster if not first monster
147+
GameObject monsterObj;
148+
if (monsters.Count == 0)
149+
monsterObj = Instantiate(monsterPrefab, leader.position, leader.rotation);
150+
else
151+
monsterObj = Instantiate(monsterPrefab, monsters[monsters.Count - 1].position, monsters[monsters.Count - 1].rotation);
152+
153+
// Get the transform of the monster
154+
Transform monster = monsterObj.transform;
155+
156+
// Make monster the child of monsterHolder and add it to monsters
157+
monster.SetParent(monsterHolder.transform);
158+
monsters.Add(monster);
159+
160+
// DEBUG: Spacebar cannot be held down
161+
spawning = true;
162+
}
163+
}
164+
}

Assets/MonsterController.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/_Project/Materials/sky.mat

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ Material:
2626
m_LightmapFlags: 4
2727
m_EnableInstancingVariants: 0
2828
m_DoubleSidedGI: 0
29-
m_CustomRenderQueue: 2000
29+
m_CustomRenderQueue: 3000
3030
stringTagMap:
31-
RenderType: Opaque
32-
disabledShaderPasses: []
31+
RenderType: Transparent
32+
disabledShaderPasses:
33+
- SHADOWCASTER
3334
m_SavedProperties:
3435
serializedVersion: 3
3536
m_TexEnvs:
@@ -84,7 +85,7 @@ Material:
8485
- _Cull: 2
8586
- _Cutoff: 0.5
8687
- _DetailNormalMapScale: 1
87-
- _DstBlend: 0
88+
- _DstBlend: 10
8889
- _EnvironmentReflections: 1
8990
- _GlossMapScale: 1
9091
- _Glossiness: 0.5
@@ -98,11 +99,11 @@ Material:
9899
- _Smoothness: 0
99100
- _SmoothnessTextureChannel: 0
100101
- _SpecularHighlights: 1
101-
- _SrcBlend: 1
102-
- _Surface: 0
102+
- _SrcBlend: 5
103+
- _Surface: 1
103104
- _UVSec: 0
104105
- _WorkflowMode: 1
105-
- _ZWrite: 1
106+
- _ZWrite: 0
106107
m_Colors:
107108
- _BaseColor: {r: 0.28627452, g: 0.78431374, b: 0.85490197, a: 1}
108109
- _Color: {r: 0.24313726, g: 0.09019608, b: 0.5372549, a: 1}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:aeb798d2af12e81b9e4a18303ef23b07c2647bd0ba35fc640835578940188835
3-
size 32997
2+
oid sha256:922a092065ce597761bbe982483fdeb9750bd9f7ef119c5d89ca9faa8c7afc6e
3+
size 31881
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:e263a7d24cc9eb2297680fe7d2b4b1df9abe9364f8d6d6252cf3dea840a7beaf
2+
oid sha256:c94528f3da0b847ff214ba541ca8bda7b965fc5acc7cac2fb2a84e4726921c18
33
size 4016
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:79228f0a04623ccb4d3b1eb1547306d04d15def58fc8656ec445fc89923e951b
3-
size 873
2+
oid sha256:8dc0fde4e989836d733575dd70c23cbf1b6915a448d293e8d867dd315b5822a0
3+
size 116727

0 commit comments

Comments
 (0)