Your Cart
Loading
unity fortune wheel tutorial unity lucky wheel how to make spin wheel in unity

Unity Fortune Wheel Tutorial | Unity Tutorial

Unity Fortune Wheel Tutorial | Unity Tutorial


In this tutorial, you'll learn how to design and implement a fun and interactive Fortune Wheel in Unity! We’ll cover the basics of creating the wheel's visual assets, writing script, setting up the spinning mechanics, and integrating prize selection logic.



WATCH FULL TUTORIAL ON YOUTUBE


CHECK OUR PROJECTS


SUBSCRIBE FOR LATEST OFFERS



=========================================================


3 Unity Projects Only $50 - ON SALE!!!!

https://youtu.be/7mI-fJpXN88


Contact: unitycoderz2022@gmail.com


=========================================================



Script:


GameManager.cs


using UnityEngine;


public class GameManager : MonoBehaviour

{

  public static GameManager Instance { get; private set; }


  private int coins;


  private void Awake()

  {

    if (Instance == null)

    {

      Instance = this;

      DontDestroyOnLoad(gameObject);

      LoadCoins();

    }

    else

    {

      Destroy(gameObject);

    }

  }


  public void AddCoins(int amount)

  {

    coins += amount;

    SaveCoins();

  }


  public int GetCoins()

  {

    return coins;

  }


  private void SaveCoins()

  {

    PlayerPrefs.SetInt("Coins", coins);

    PlayerPrefs.Save();

  }


  private void LoadCoins()

  {

    coins = PlayerPrefs.GetInt("Coins", 0);

  }

}


------------------------------------------------------------------------------------------------------------------------------------------


WheelController.cs


using UnityEngine;

using System.Collections;

using UnityEngine.UI;

using System.Collections.Generic;


public class WheelController : MonoBehaviour

{

  public float initialSpinSpeed = 2000f;    // Initial spin speed

  public float deceleration = 0.98f;      // Deceleration factor

  public Transform wheel;            // Reference to the wheel Transform

  public Text resultText;            // UI Text to display result

  public Text coinBalanceText;          // UI Text to display coin balance

  public Transform pointer;           // Reference to the pointer Transform

  public Button spinButton;           // Reference to the spin button


  private bool isSpinning = false;

  private float currentSpinSpeed;


  // Use List<int> for easier editing in Inspector

  public List<int> coinValues = new List<int> { };

  // Corresponding angles for each coin value

  private float[] segmentAngles;


  void Start()

  {

    UpdateSpinButtonState();


    // Calculate the segment angles based on their positions

    segmentAngles = new float[coinValues.Count];

    for (int i = 0; i < coinValues.Count; i++)

    {

      segmentAngles[i] = (360f / coinValues.Count) * i;

    }

  }


  void Update()

  {

    coinBalanceText.text = "Coins: " + GameManager.Instance.GetCoins();

  }


  public void StartSpin()

  {

    if (!isSpinning)

    {

      spinButton.interactable = false; // Disable the button

      StartCoroutine(SpinWheel());

    }

  }


  private IEnumerator SpinWheel()

{

  isSpinning = true;

  currentSpinSpeed = initialSpinSpeed;


  while (currentSpinSpeed > 0.1f)

  {

    wheel.Rotate(0, 0, currentSpinSpeed * Time.deltaTime);

    currentSpinSpeed *= deceleration; // Apply deceleration

    yield return null;

  }


  CalculateResult();

   

  // Re-enable the spin button after spinning stops

  spinButton.interactable = true;


  isSpinning = false;

}



  private void CalculateResult()

  {

    float finalAngle = (wheel.eulerAngles.z % 360) + UnityEngine.Random.Range(-10f, 10f); // Add randomness

    int segmentCount = coinValues.Count;


    int segmentIndex = -1;


    for (int i = 0; i < segmentCount; i++)

    {

      if (finalAngle >= segmentAngles[i] && finalAngle < segmentAngles[(i + 1) % segmentCount])

      {

        segmentIndex = i;

        break;

      }

    }


    if (segmentIndex == -1 && finalAngle >= segmentAngles[segmentCount - 1])

    {

      segmentIndex = segmentCount - 1;

    }


    int coinsWon = segmentIndex != -1 ? coinValues[segmentIndex] : 0;


    GameManager.Instance.AddCoins(coinsWon);

    resultText.text = "You won: " + coinsWon + " coins!";

    StartCoroutine(BlinkResultText());

  }


  private void UpdateSpinButtonState()

  {

    spinButton.interactable = true; // Always enable button for spinning

  }


  private IEnumerator BlinkResultText()

  {

    CanvasGroup canvasGroup = resultText.GetComponentInParent<CanvasGroup>();

    if (canvasGroup == null)

    {

      canvasGroup = resultText.gameObject.AddComponent<CanvasGroup>();

    }


    float duration = 0.2f; // Duration for fade in/out

    int blinkCount = 6;  // Number of times to blink


    for (int i = 0; i < blinkCount; i++)

    {

      for (float t = 0; t < duration; t += Time.deltaTime)

      {

        float alpha = Mathf.Lerp(1, 0, t / duration);

        canvasGroup.alpha = alpha;

        yield return null;

      }


      for (float t = 0; t < duration; t += Time.deltaTime)

      {

        float alpha = Mathf.Lerp(0, 1, t / duration);

        canvasGroup.alpha = alpha;

        yield return null;

      }

    }


    canvasGroup.alpha = 1; // Reset to fully visible

  }

}





DOWNLOAD CODESTER FILES