Learning Unity Basics + Prototyping

After watching the learn to play page for Arrow Caverns, I felt I had a good enough grasp on things to get started. If you haven’t played the game, I recommend taking a look at that before reading further, as this blog will assume that you have an understanding of the board game when talking about how I’m trying to translate it into Unity.

The first step was to just get things setup correctly. Having a portrait orientation for mobile devices, and setting our target device as iOS for now.

Next, I wanted to figure out how to just get things roughly looking like the paper game. Spawn a 5x5 grid of arrows, and then orient them randomly.

Getting sprites to display was pretty straightforward- I arranged the game objects into a grid and used the Instantiate function to spawn them once the game loads and changed their Scale to be sized appropriately. However, getting them to randomly spawn facing up, down, left, or right took a bit of searching. At first I wanted to randomly get values of either 0, 90, 180, 270 degrees to rotate the object’s Z-axis orientation on start, but wasn’t sure how to do that. So instead, I randomly chose a number between 1 and 4 and then assigned those to the different Z rotation amounts. After some tweaking, I was able to spawn a grid of arrows!

Next, I wanted to learn how to make UI to display the turn indicator and current phase. Also threw in a temporary background to make it look more “cave-y” and a couple placeholder spelunkers. At this time, both indicators cycled through when you press the spacebar, but now I know I can assign some different condition to cycle through the phase display at a later time.

Lastly, I found some code online which I attached to the spelunkers, allowing me to drag and drop them around the screen. At some point, I’d like to figure out how I can get them to snap to the arrows, and also be limited to the arrows they’re supposed to be able to move to.

This was thrown together over a couple days, and I knew that a lot was hastily done and messy. So I created a new unity project to try and test my memory and clean things up a bit.

I realized that Instantiate wasn’t necessary, and instead just started the arrows off where I wanted them to be. After rebuilding the scene, the first new task I gave myself was to figure out how to have the arrows spin 90 degrees once tapped upon. This thread gave me some code to work with to detect when an object was tapped on, but when I attached it to my arrows and tapped on one, every arrow rotated 90 degrees.

Every arrow had a script which on every frame, checked whether the tap on screen was touching a game object collider before executing code to rotate 90 degrees. So no matter which collider you touched, every arrow would see that condition as true (a game object with a collider was touched), and execute the code within (rotate). So, I had to add in a line which verified that the name of the touched object equaled the name of the arrow itself, and then allowed it to rotate. Other arrows will still detect that you’re touching a collider, but won’t rotate because the touched object name will not be the same as the other arrow names. This one was super satisfying to figure out, and I’m sure there’s a much better way to do this too. But for now, it works- maybe I’ll come back to it later.

If anyone knows of a nice way to convert a video to a .gif, let me know. Sorry for the choppiness!

Next, I wanted to figure out how to change the sprite displayed on a game object. In Arrow Caverns, if a Spelunker is on the Buried side of a cavern tile during the Excavate phase, the tile is flipped to either reveal a Treasure, or an Unstable arrow pointing in the same direction as the Buried tile.

At first I thought I’d have to create a new game object to switch the image, but instead you can just write a script which sends a new sprite to the Sprite Renderer component of your object! (Resources used: 1, 2)

Testing the ability to update an object’s sprite. In this example, changing the arrow to the Unstable sprite.

Now I can set a condition for the script that changes the sprites where: IF it is the Excavate phase, AND a spelunker is on the tile, AND it has been marked as a treasure tile, switch the sprite to Treasure and mark that treasure as found. ELSE IF it is the Excavate phase AND a spelunker is on the tile, switch the sprite to the Unstable side. At some point I’ll have to handle game over conditions (spelunker is on unstable side when the excavate phase is entered).

Next up was to improve the UI to properly represent the turn number and current phase. I wanted to include a button which would move between phases, and when the End/Escape phase was up, tick the turn counter up and start from the beginning. This allowed me to use the variable associated with each phase to control behavior of other game objects. For example, arrow tiles can only be rotated during the Survey phase.

However, I had absolutely no idea how to reference a variable created in another object/script LOL. This guide on the topic was super helpful, and was a real level up moment for me with Unity.

Now, there’s a button which runs through the phases (and updates to say “End Turn” when you’re on the last phase!) and when you cycle back to the first phase, it tells the Turn display to tick up by one!

*Note: Just thought of another game over condition task: ticking over to turn 8.

Also tossed in some meeple spelunkers which start off at the center of the screen. I’ll toss that drag and drop code onto them at some point, but I’m thinking they’ll be a little complicated for me to figure out how to get them to ONLY stay on the proper tiles.

The last task I completed during this round of work was coming up with a way to randomly decide which tiles have treasure underneath. I labeled my arrows 1-24 reading from left to right, top to bottom, so I figured if I get 4 random numbers from 1 to 24 (no duplicates), during the excavate phase I can have the game object check if its name matches up with one of the chosen treasure numbers. If it does and a spelunker is on it, it’ll turn over to treasure! I’m realizing I could also have a boolean for each variable that can be swapped between True and False, representing whether the treasure has been found.

Regardless, I had fun thinking of this script which checks to make sure there are no duplicates as it generates the four treasure locations. Again, I figure there’s gotta be a quicker way to do this, but it works as I imagined!

I’m realizing now that those private variables will need to be public if I am to call them to check if a tile has a treasure!

And that’s about as far as I got! Some next tasks include spelunker movement, sprite changing and treasure finding, win/game over conditions, and I’m sure plenty other things I can’t think of at the moment.

If anyone happens to read this and thinks it’d be helpful to have more code snippets included, I’d be happy to share those! I just figured pics/gifs and thought process may have been interesting for readability. But open to all feedback.

Thanks for reading!

Previous
Previous

Pesky Spelunkers

Next
Next

Getting Started