Unity Slot Machine

Slot Machine Demo

MK - Realistic Slot Machine This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers. Thanks to the time-restricted nature of a game jam, they opted to buy a 3D slot machine asset off of the Unity Store (a marketplace that lets game makers pay modelers and animators for unrestricted.

Classes

  • Reel (Assets/SlotGame/Scripts/Reel.cs)
    It have sprite renderers as childs and update renderers based on internal cursor values(List).
    When ‘ReelStatus’ is set to moving, ‘moveValue’ is increased and cursor values is updated automatically.
    Then renderers is updated using ‘OnRenderUpdated’ event callback function.

  • Machine (Assets/SlotGame/Scripts/Machine.cs)
    It provides a common interface for slot machines and have reels as childs.

CREATE BY

  • Sanghun Lee
  • Email: tkdgns1284@gmail.com
  • Github: https://github.com/bss1284
Jump To:
  1. Scene Setup
  2. Scripting

Objective

In this blog post, I’ll show you how you can implement a spin wheel/wheel of fortune in your game.

Step 1 Introduction to Wheel of Fortune

I guess, you already know what Wheel of Fortune is.

If you’re fond of playing Candy Crush, you should have already spun that Daily Booster Wheel countless times.

Spin & Win from 8 Ball Pool is also quite popular among gamers.

Or,

You might have seen it on some reality TV game shows, where a person spins a wheel to get a prize or a task to win the prize.

Do you want similar kind of wheel in you game?

If yes! Then you have come to the right place!

It’s quite simple, but you might find a couple of things tiny bit tricky and confusing.

Worry not! I will try to make it simple as I can.

Let’s get rolling then.!

Step 2 Scene Setup

Let's Setup the Scene. Red stag casino login.

2.1 Wheel Setup

  • Set your wheel in the scene view. Make sure the partitions on the wheel are of same size.
  • Set small circle colliders on the edges (you need to create another empty GameObjects to store colliders) and set them as children of your wheel GameObject.
  • Now to keep the hierarchy neat & tidy you can create another empty GameObject as parent to the collider objects.

2.2 Arrow Setup

Unity Slot Machine Animation

  • I’m taking a simple black cube to represent an arrow; you can use any of your favorites.
  • Add a BoxCollider2D & a HingeJoint2D component to your Arrow object.

Unity Slot Machine 3d

Machine

If you’ve never encountered HingeJoint component before, it works as a hinge joint like we use in doors & windows.

Spend some time to play with its properties to get your desired result.

You can also refer to the Unity Docs for more detailed descriptions:

We’ve completed the scene setup now.

It should look something like this:

Step 3 Scripting

This time there’s only one script for us to worry about. (That’s a good thing, right?)

Note

Before I start explaining the code, attach the script to your wheel GameObject so that you can see the effect of code. That will make things easier to understand.

3.1 Animation Curves

As you can see in the code I’m using Animation Curves to set the wheel in motion. You can set a variety of shapes in these curves to have various kinds of motions for your wheel.

Animation Curves look like something below:

To access the curves, click on the curve in the inspector tab.

Unity Slot Machine Source Code

Also DO NOT forget to set the proper size of prize list.

Now to Update() method:

  • It contains 2 random numbers and a coroutine call: SpeenTheWheel()
  • itemNumber sets the item which will be under the arrow when the wheel stops.
  • maxAngle (or finalAngle)denotes what the final angle will be.

[Yes! We’ll know the result before spinning the wheel. But don’t worry; it’s still a mystery for our players.]

Let's check SpinTheWheel() coroutine:

  • At first the timer, startAngle and maxAngle are initialized.
  • After that a random animation curve is selected.

Unity Slot Machine Source Code

And the while loop inside:

  • The code inside is to calculate angle (rotation in z) for the wheel.
  • Here Evaluate() of animation curve gives the value of curve for given time.

The spinning logic for our wheel ends here.

Step 4 Conclusion

Now it’s up to you how to use the prize on the itemNumber that we get at the end (which we knew from the beginning :) ).

Now you know why we never got that so called 'BIG PRICE' or 'JACKPOT' EVER.!!

Now hit play and spin the wheel to see what you can win (Don’t cheat :P).

Simple isn’t it? If you still have any doubts leave a comment & I’ll be back (Just like Arnold Schwarzenegger would say!).

Also check out our other awesome Unity Tutorials right here.

Got an Idea of Game Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Unity 3D Game Development Company in India.

Created on : 07 July 2016

An Addictive Gamer turned into a Professional Game Developer. I work with Unity Engine. Part of TheAppGuruz Team. Ready to take on Challenging Games & increase my knowledge about Game Development.

PREVIOUS POST
Beginner’s Guide: Learn to Make Simple Virtual Joystick in Unity
NEXT POST
The Mystery of WaitUntil & WaitWhile in Unity 5.3 Revealed!!