|
| 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 | +} |
0 commit comments