Let's Create: Whack-A-Mole in Unity

  Рет қаралды 8,756

Firnox

Firnox

Күн бұрын

In this Unity tutorial we create Whack-A-Mole which has a grid of 9 moles that pop up at random and you have to tap on the pesky critters to get rid of them.
We create the logic for each mole/bomb interaction in one script, and have a separate GameManager script orchestrating the game logic. This covers topics such as: UI, Sprites, Sprite Animation, Sprite Masks, Lerp and BoxCollider2D.
As with all the Let's Create tutorials in the series, we'll focus on creating the core game mechanic, leaving the extra polishing to you. If you have any request for small games you'd like to see in this series please leave a comment.
The full code for this is available on GitHub: github.com/Firnox/Whack-A-Mole
Other videos in the Let's Create series:
1. 2D Sliding Puzzle - • Let's Create: A slidin...
2. Whack-A-Mole - [this video]
3. Block stacking game - • Let's Create: A block ...
4. Minesweeper - • Let's Create: Mineswee...
5. Asteroids - • Let's Create: Asteroid...
Contents:
0:00 Introduction
1:40 Creating the mole
5:46 Handling OnClick logic
7:06 Adding hard hat mole type
8:25 The bomb animation
9:35 Adding level difficulty
13:30 GameManager logic
18:45 Adding game UI

Пікірлер: 44
@alexandertan2337
@alexandertan2337 Жыл бұрын
Hi Firnox! Thank you for this wonderful tutorial.
@tinylaly
@tinylaly Жыл бұрын
Thanks this is really good
@ievarituma1918
@ievarituma1918 Жыл бұрын
Nice tutorial, ty
@anete776
@anete776 Жыл бұрын
Amazing content!
@hadyanihtifazhuddin3476
@hadyanihtifazhuddin3476 Жыл бұрын
thanksss for the content
@tmjaye5536
@tmjaye5536 Жыл бұрын
Hi, I have completed the tutorial (which is great Thank You) and would like to put sound to the moles and another to the bombs when you click on them. As I am a beginner, I have no idea where I should put the reference, would I place it in the Game Manager script, or would I need to add a new script altogether. Thank you again for the tutorial, it has been very helpful 🙂
@FirnoxGames
@FirnoxGames Жыл бұрын
Firstly, you want to add a "Audio Source" component (which you can put on your GameManager). The in the GameManager you'll need a reference to this: `public AudioSource audioSource;`, you'll also want to add a couple of audio clips to represent your sounds: `public AudioClip moleSound; public AudioClip bombSound`. Then when you handle the mole click with the "AddScore" function you can put in the playing of the sound there `audioSource.PlayOneShot(moleSound)`. Clicking on the bomb is handled in "GameOver" so on the line before setting the bomb text you can put the other `audioSource.PlayOneShot(bombSound)`. Hope that helps!
@tmjaye5536
@tmjaye5536 Жыл бұрын
@@FirnoxGames OMG Thank You 🥳 I have been stuck on that for days, I was kind of close, but also waaaay off 🙃 Every time I tried something, I kept messing everything else up. Thank you so much again for all your help, it is very much appreciated.
@user-fx6eg1ic5v
@user-fx6eg1ic5v 8 ай бұрын
Thanks for the tutorial. I don't know how the bug is caused but the moles stop spawning after a while.
@Lucas-ed6dv
@Lucas-ed6dv Жыл бұрын
Very clear tutorial, so thank you very much in advance. However, im a beginner at Unity and i want to create a project using this code. Would It be possible to "hit" the moles by pressing buttons on the keyboard? So instead of using the mouse. Kind regards!
@FirnoxGames
@FirnoxGames Жыл бұрын
Yes absolutely that would be possible. First thing in your Mole.cs script change the OnMouseDown() function to public so you can call it from the GameManager. Then in the GameManager in the Update method you can check for your key presses and map them to the moles. I added the moles in a strange order to the array so center=0, topmiddle=1 bottommiddle=2. But once you have the mapping you can just do a big if else loop to sort it out: if (Input.GetKeyDown(KeyCode.Keypad5)) { moles[0].OnMouseDown(); } else if (Input.GetKeyDown(KeyCode.Keypad8)) { moles[1].OnMouseDown(); } else if (Input.GetKeyDown(KeyCode.Keypad2)) { moles[2].OnMouseDown(); }...
@skyfeedy
@skyfeedy Жыл бұрын
Thanks for the lesson! But I have had difficulties since 11:05 - my mole does not appear after it goes into the hole. What should I do in this situation?
@FirnoxGames
@FirnoxGames Жыл бұрын
Skyfeed, sorry for the confusion. I think at this point in the video the code is such that it comes up and down once. We call the ShowHide Coroutine in the start method. After the model has finished hiding it won't come up again until it gets called again. As we go through and implement the game logic we call the method repeatedly to keep the right number of moles visible, at around 12:38 I convert the Start function to an Activate function so we can repeatedly call it. I missed out explaining that I'd looped the animation, to be able to show you the need to shrink the collider. I think if you carry on with the video you should be fine. Sorry!
@skyfeedy
@skyfeedy Жыл бұрын
@@FirnoxGames I converted the "Start" function to the "Activate" function, but in the end even the animation stopped working. After converting to Activate, I can click on the moles and they disappear, but there is no animation of their appearance What did I do wrong again?
@FirnoxGames
@FirnoxGames Жыл бұрын
@@skyfeedy Sorry it's hard to know what you have and haven't done. The GameManager is the bit that calls the Activate function, the full code is available on GitHub: github.com/Firnox/Whack-a-mole so you could just copy/paste the code for the Mole and GameManager in there into your project and see if that fixes things. If not then there is possibly something missing from the Unity Inspector end. You can just download the full project as a zip file and load it up in Unity then you could compare to see where the issue is, or just start from the finished product if you'd rather.
@madeleineyeung5902
@madeleineyeung5902 Жыл бұрын
This was a great tutorial! However, I'm having a little bit of trouble. I finished adding in all the GameManager logic, but my game's play button does not work. Nothing happens after I click my "Play Game!" button. Additionally, my moles do not pop up and down after I hit the play button in Unity, as they simply remain static. The only thing that seems to be working is when I click each mole, they change to the HitMole sprite and disappear from the screen completely. I am a beginner at code/Unity so I fear I may have messed up somewhere...
@FirnoxGames
@FirnoxGames Жыл бұрын
Sorry that you've been having issues, it's probably the case that you're just missing a link tying together the objects and the game manager but it's hard for me to make suggestions without seeing your code. You can download the full project from the video here: github.com/Firnox/Whack-A-Mole/archive/refs/heads/main.zip
@madeleineyeung5902
@madeleineyeung5902 Жыл бұрын
@@FirnoxGames Do you happen to have the file with the game already set up? I downloaded the zip file with all of the assets, but nothing was set up in the Unity Scene. I think my issue lies with how I connected things in the Inspector, but I'm not sure where I messed up.
@FirnoxGames
@FirnoxGames Жыл бұрын
@@madeleineyeung5902 Sorry for the delay in replying. I'm sorry for the confusion, the scene is there setup everything in it however when you download and open the project it just shows you a new scene. If you go to the Scenes folder and double click on the "Sample Scene" it'll load up the one I did in the video with everything linked and should just work for you.
@madeleineyeung5902
@madeleineyeung5902 Жыл бұрын
@@FirnoxGames Ahh I see, I didn't realize! Once again, I'm a complete beginner. I see it now. Thank you so much! :)
@RuthlessMetalYT
@RuthlessMetalYT Ай бұрын
when the game isn't running the moles look fine but when I start the game they are hardly visible in the hole. How do I change that and put them higher up?
@RuthlessMetalYT
@RuthlessMetalYT Ай бұрын
The moles needs to be higher up upon plopping out of the hole. How do I change that?
@shortvideos8177
@shortvideos8177 Жыл бұрын
In your previous tutorial I got confused whenever you remove the reshuffling method the game misbehaves
@FirnoxGames
@FirnoxGames Жыл бұрын
I'm sorry I don't understand your issue. On the SlidingPuzzle game, how are you removing the reshuffling method. If you just comment out the StartCoroutine(WaitShuffle(0.5f)); on line 61. Then the game will allow you to click on pieces, and it should still work. It just wouldn't do anything when you finish the game.
@shortvideos8177
@shortvideos8177 Жыл бұрын
@@FirnoxGames hello after doing that the game does not shuffle on start
@shortvideos8177
@shortvideos8177 Жыл бұрын
@@FirnoxGames hello i commented that out on. Start the game does not shuffle
@camileytor3000
@camileytor3000 Жыл бұрын
This is a great tutorial! however I've found a bug, I'm not quite certain what could've caused it but the moles son't come back up after they've gone down the first time around
@FirnoxGames
@FirnoxGames Жыл бұрын
The moles once triggered go up and down once only, then they need to be triggered again. In the GameManager this is done in the Update() method where if we've got space on the board we trigger another mole by calling the Activate function: moles[index].Activate(score / 10); If you have this then the only thing I can think of is that you didn't assign the GameManager to each of the mole objects. Without that when the animation finishes they need to tell the GameManager you missed them, so if that assignment is missing it wouldn't work - though that should give you an error.
@dany3310
@dany3310 8 ай бұрын
Yes, I have the same problem here.
@RuthlessMetalYT
@RuthlessMetalYT Ай бұрын
Help, my moles are barely visible when plopping up. Spent a long time trying to figure out what's wrong but I just can't manage to understand what's wrong.
@RuthlessMetalYT
@RuthlessMetalYT Ай бұрын
I figured it out, I didn't put -1 on the Y axis on the hole. I'm a dummy. haha
@FirnoxGames
@FirnoxGames Ай бұрын
Glad you managed to figure it out on your own!
@RuthlessMetalYT
@RuthlessMetalYT Ай бұрын
@@FirnoxGames yeah it took some time. Unity is time consuming. thanks for the video!
@RuthlessMetalYT
@RuthlessMetalYT Ай бұрын
How do I add different types of moles?
@Proseph323
@Proseph323 Жыл бұрын
This tutorial is amazing, just can't get my moles to respawn or my play button to function. I'm sure I messed up somewhere along the way.
@camileytor3000
@camileytor3000 Жыл бұрын
I'm having the exact same issue
@FirnoxGames
@FirnoxGames Жыл бұрын
For the play button, the only thing I can think is that missed the assignment of the function to the onclick event at point 17:35 in the video?
@mudassarkhan6153
@mudassarkhan6153 6 ай бұрын
How can I implement Touch Screen Input in this game please help.
@FirnoxGames
@FirnoxGames 6 ай бұрын
As it was implemented using the mouse events this should work for single touches, because in Unity the first touch acts just like a mouse click. However, it will not work for multiple touch events working at once then you'll have to look into the touch options: docs.unity3d.com/ScriptReference/Input.GetTouch.html
@gytiskirkliauskas2482
@gytiskirkliauskas2482 Жыл бұрын
I dont know why but my sprite mask always becomes a circle
@FirnoxGames
@FirnoxGames Жыл бұрын
On the SpriteMask component I use a 1x1 white pixel as the sprite and set the scale to 256x256 to give the right sized square sprite mask. Have you perhaps used some other sprite in here (such as the Knob which is circular)?
@4lfinity
@4lfinity Жыл бұрын
Im Confusing Broo
@FirnoxGames
@FirnoxGames Жыл бұрын
I've got the project linked in the video description which you can download from GitHub if looking at the full finished thing will help you out!
I made a game using Godot for the first time
7:35
shawcat
Рет қаралды 1 МЛН
Let's Create: Minesweeper in Unity
20:41
Firnox
Рет қаралды 1,9 М.
Bro be careful where you drop the ball  #learnfromkhaby  #comedy
00:19
Khaby. Lame
Рет қаралды 43 МЛН
Pray For Palestine 😢🇵🇸|
00:23
Ak Ultra
Рет қаралды 32 МЛН
2 Python Developers VS $1000
15:04
PolyMars
Рет қаралды 1,7 МЛН
Let's Create: Asteroids in Unity
31:59
Firnox
Рет қаралды 3,2 М.
How To Make A Video Game - GDevelop Beginner Tutorial
28:04
GDevelop
Рет қаралды 22 М.
Whack a Mole Game in Javascript HTML CSS
21:56
Kenny Yip Coding
Рет қаралды 75 М.
What size should your assets be? | HD 2D GAME ART
12:10
Nonsensical 2D
Рет қаралды 100 М.
2D water magic
10:21
Steve Mould
Рет қаралды 482 М.
Pokémon Yellow - analysis of glitch sprite decompression effects
17:09
What Jumping Spiders Teach Us About Color
32:37
Veritasium
Рет қаралды 1,5 МЛН
What are Events? (C# Basics)
15:05
Code Monkey
Рет қаралды 372 М.
Make Video Games
44:12
Pirate Software
Рет қаралды 943 М.