Sunday, 15 July 2018

How to Make a Wheel of Fortune in Unity 2018 [ free code + assets ]

Watch Video Tutorial On Youtube

Spinner Script


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class spinner : MonoBehaviour {

public float reducer;
public float multiplier = 1;
bool round1 = false;
public bool isStoped = false;


void Start ()
{
reducer = Random.Range (0.01f, 0.5f);
}
// Update is called once per frameQ
void FixedUpdate () {

if (Input.GetKey (KeyCode.Space)) 
{
Reset ();
}

if (multiplier > 0)
{
transform.Rotate (Vector3.forward, 1 * multiplier);
} else
{
isStoped = true;
}

if (multiplier < 20 && !round1) 
{
multiplier += 0.1f;
} else 
{
round1 = true;
}

if (round1 && multiplier > 0)
{
multiplier -= reducer;
}


void Reset()
{
multiplier = 1;
reducer = Random.Range (0.01f, 0.5f);
round1 = false;
isStoped = false;
}
}

Needle Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class needle : MonoBehaviour {
public spinner _spinner;
public Text scoretext;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}

void OnTriggerStay2D(Collider2D col){
if (!_spinner.isStoped)
return;
scoretext.text = col.gameObject.name;

}

}

6 comments:

  1. thank u man for this gift , just i want to ask u if i want the wheel staring spin only if i click it on a button how i can do that (i know that should to add button to Hierarchy & drop the script spinner to the On Click in Inspector to button. i literally do that but no function showing up )
    and sorry for this dumbest question i'm new in unity in c# scripting and thank u so much

    ReplyDelete
    Replies
    1. Idiots! keep rocking!!! May our Fortune spin like a Mad slippery wheel...

      Delete
  2. you have to put that Reset() function in your button clock event.
    if you are not getting function against script name in click event use public before Reset().

    ReplyDelete
  3. Its works, but at the start it spin without button, woh to fix it? plz help

    ReplyDelete
  4. notice that these two value should be
    public float multiplier = 0;
    bool round1 = true;
    and the function should be
    public void Reset()
    after that you can trigger it by button

    ReplyDelete