Showing posts with label 2018. Show all posts
Showing posts with label 2018. Show all posts

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;

}

}

Thursday, 17 May 2018

how to use slider in unity


first goto -> create -> UI -> slider and create a slider this will create a canvas if not already created.
create a script and attach it to any gameobject (payer or camera) or just put the code in any script
assing the public slider variable in inspector by draging the slider component from hierarchy to inspector as shown below.

now run the code after running you will notice the slider value will change to 10 as written in code.

Friday, 23 February 2018

Random tiles Generation in Unity 2d game at runtime

 Watch Video on youtube

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

public class TilesGenerator : MonoBehaviour
{
    private Camera mainCam;
    public GameObject Player;
    public GameObject[] Tiles;
    float spawnY;
    // Use this for initialization
    void Start () {
        mainCam = Camera.main;
        spawnY = mainCam.transform.position.y + 3.0f;
    }
   
    // Update is called once per frame
    void Update ()
    {
        if (mainCam.transform.position.y < Player.transform.position.y)
        {
            Vector3 targetPos = new Vector3
                (
                    mainCam.transform.position.x,
                    Player.transform.position.y,
                    mainCam.transform.position.z
                );

           mainCam.transform.position = Vector3.Lerp
                (
                    mainCam.transform.position,
                    targetPos,
                    0.5f
                );
        } //endif

        if (mainCam.transform.position.y > spawnY)
        {
            spawnY += Random.Range(3,4.5f);
            float spawnX = Random.Range(-5f,5f);
            SpawnTile(new Vector3(spawnX, spawnY));
        }
    }

    void SpawnTile(Vector3 pos)
    {
        pos.z = -0.99f;
        int RandomTile = Random.Range(0, Tiles.Length - 1);
        Instantiate(Tiles[RandomTile],pos,Tiles[RandomTile].transform.rotation);
    }
}