In class this week we expanded on the previous project with mouse clicking and moving objects, and instantiating a prefab.
Now there is functionality to select multiple units by left clicking (and holding) with the mouse and dragging the cursor to a second location in the game scene. If units are inside of the transparent green zone when the left mouse button is released it selects the units inside. And while these units are selected, right clicking somewhere in the game scene moves all the units that are selected to that location.
This is done by having two variables for mouse positions. When the left mouse button is clicked it casts two rays and instantiates a transparent box collider that is a trigger. One raycast stores the original hit position and the second is always the current location of the mouse. The transparent selection trigger scales between the two locations of the mouse positions. If any unit’s (game objects with the “Player” tag) are being touched by this transparent trigger box collider (OnTriggerEnter), they’re added to a list. On exit (OnTriggerExit) they’re removed from the lust.
Then to move them, instead of only having the one game object’s move to the mouses right click location, the script does a foreach loop. For each game object in the list. They move to the mouses right click location. If the list is empty, nothing happens.
We also added a button that calls the method to instantiate the prefab for the building, rather than just pressing ‘F’. But as I had already figured this out in the previous week. But something new was when the prefab is instantiated and the raycast is hitting certain objects that doesnt enable the building to be placed it turns red and the building is unable to be placed. When the building is placed it plays a little raise animation that we created.
+ Bounds for the camera to stop at so it doesn’t continuously scroll.
+ Some textures and learnt how to paint with them && a second landscape on the outer of the landscape. It’s surprising how much flavour this gives to the game.
+ Boundaries so the camera cant move past certain dimensions. This is exactly the same as what we had implemented in the week 2 SHMUP project with the ship. Have 4 variables that are int’s and give each a value relative to a distance in the world space. Two int’s that will store a location for a Z distance and two for the X. So in this case. My landscapes have positioned so that the centre is exactly at (0,0,0, (x,y,z)) The four variables that i never want the camera to move past are 60 and negative (-)55 on the Z axis, and 55 and negative (-)55 on the X axis. In the update function if the camera’s position is ever equal to or greater than any of those set int’s it returns the camera position to that int that its trying to be greater than.
+ A grid for towers to be placed to. 1313 grids to be exact.Each grid is just a box collider that is a trigger. If they weren’t the enemies wouldn’t be able to run through the landscape. There would literally be a giant wall blocking them. When a tower is in the stage where it is ready to be placed, it still constantly follows the mouse position, but when it’s hovering over a grids location it will stick to that grids position to show exactly where it were would sit if it was to be placed on that grid. If the mouse isn’t hovering over a grid while a tower is still being placed, the tower follows the exact location of the mouses’ position.
+ A Checkpoint system for the enemies to run through.
Each checkpoint is a box collider that is a trigger. Each enemy now contains a bool that is false for each of the checkpoints. When an enemy enters a checkpoint, the checkpoint accesses the enemy’s script and turns the bool of that checkpoint to true.
When each enemy spawns, they roll a random number between 0 and 2 (because unity’s Random.Range maximum number is exclusive, which means it will only generate a number that is anywhere between the lowest number and 1 before the maximum number), and find the closest checkpoint to their location and start running towards it.
In an the update function the enemies have a method that makes them RunTheGauntlet() which makes them set their destination to one of the checkpoints depending on which bools are true.
If they touch the first left checkpoint, the destination changes to the bottom left checkpoint, the mirror for the right side. When they touch either of the bottom of the checkpoints, they run to the bottom middle checkpoint. Finally when they touch the bottom middle checkpoint, they run to the final destination (marked with the orange glowy circle).
But what happens if they spawn in the middle you ask? That’s where the random roll comes into action. When they touch that checkpoint, if the number is 0, they go left. If it’s 1, they go right. Tadaaa.
A small problem that I encountered with the random roll was that I originally had it in the RunTheGauntlet method (which is running every frame). So each frame the enemy is constantly rolling a random number, then when they reach the middle checkpoint the number that is supposed to be a solid 0 or 1 was constantly changing, causing them to be undecided in which checkpoint they should run to. Eventually they would have made it to a checkpoint and continued on. But a simple fix was to call the random roll once when they spawn.
+ Changed navmesh radius on enemy, builder and gold collector so they don’t need to much room to move, so they can overlap and move more smoothly instead of getting stuck.
+ Projectiles for each tower.
+ Wave timing and spawning certain enemies each wave.
Each spawn location holds an array of size (11) to store each enemy, along with a number of enemies to spawn. Every time a wave starts a single type of enemy spawns. The spawner’s choose the same number in the array depending on which wave it is, to spawn that specific enemy.
+ A Coroutine when the game starts and when each wave is finished to start a countdown timer for the next wave to spawn.
I’ve met all of my plans that I wanted to from last week.
Final decision on which texture to use for the landscape 😛
A small amount of time spend on the random roll bug.
Figuring out correct syntax to make the towers that i’m placing stick to the grids location. (Once I figured that out, making the towers location do the sticky grid, while the cursor was moving was easy)
Constant null reference exception errors popping up if the camera’s raycast isn’t hitting anything.
Next weeks plans
Game over menu.
Main game scene.
Try to work out if the enemies NavMesh path is blocked and put them into an attacking phase.
Clean up a few inconsistencies.
This weeks song:
Until next time –
Technologies, U. (2015). Unity – Scripting API:. Docs.unity3d.com. Retrieved 1 December 2015, from http://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
Technologies, U. (2015). Unity – Scripting API:. Docs.unity3d.com. Retrieved 1 December 2015, from http://docs.unity3d.com/ScriptReference/Collider.OnTriggerExit.html
Msdn.microsoft.com,. foreach, in (C# Reference). Retrieved 1 December 2015, from https://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx
Technologies, U. (2015). Unity – Scripting API:. Docs.unity3d.com. Retrieved 1 December 2015, from http://docs.unity3d.com/ScriptReference/Random.Range.html
Technologies, U. (2015). Unity – Scripting API:. Docs.unity3d.com. Retrieved 1 December 2015, from http://docs.unity3d.com/ScriptReference/Coroutine.html
Lyness, N. (2015). Multi Select Screenshot. Retrieved from https://feenikxfire.wordpress.com
Lyness, N. (2015). Multi Select Move Screenshot. Retrieved from https://feenikxfire.wordpress.com
Lyness, N. (2015). Building Can Place Screenshot. Retrieved from https://feenikxfire.wordpress.com
Lyness, N. (2015). Building Can’t Place Screenshot. Retrieved from https://feenikxfire.wordpress.com
Lyness, N. (2015). Tower Defense Updated Landscape Screenshot. Retrieved from https://feenikxfire.wordpress.com
Lyness, N. (2015). Grid Layout Screenshot. Retrieved from https://feenikxfire.wordpress.com
Lyness, N. (2015). Checkpoint Screenshot. Retrieved from https://feenikxfire.wordpress.com
EpitaphRecords,. (2015). Parkway Drive – “Dark Days”. Retrieved from https://www.youtube.com/watch?v=_7lpftvYDMA