Unity: Create a Simple Board Game

Roll a dice and move forward

Introduction

Prerequisites

  1. Basic knowledge of Unity C# programming

Steps

Our very first step
You can select any colour
Blue means Prefab
The white capsule is our character
7 Dice images
Settings for the button image
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameSceneController : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}
// Update is called once per frame
void Update()
{

}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameSceneController : MonoBehaviour
{
private float stepX; // Start is called before the first frame update
void Start()
{
stepX = 0;
}
// Update is called once per frame
void Update()
{

}
public void RollDice()
{
int result = Random.Range(1, 6);
Debug.Log("Dice rolled: " + result);
stepX += 1.1f * result;
}

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameSceneController : MonoBehaviour
{
public GameObject character;
private float stepX;
private int speed = 3;
// Start is called before the first frame update
void Start()
{
stepX = 0;
}
// Update is called once per frame
void Update()
{
Vector3 target = character.transform.position;
target.x = stepX;
character.transform.position = Vector3.MoveTowards(character.transform.position, target, Time.deltaTime * speed);

}
public void RollDice()
{
int result = Random.Range(1, 6);
Debug.Log("Dice rolled: " + result);
stepX += 1.1f * result;
}
}
  1. obtains the position of the character,
  2. change the X position to the value of stepX
  3. Use the Vector3.MoveTowards() function to change the character position smoothly.
Button On Click settings
Game Scene Controller settings
Main Camera settings
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameSceneController : MonoBehaviour
{
public GameObject character;
public Sprite dice0;
public Sprite dice1;
public Sprite dice2;
public Sprite dice3;
public Sprite dice4;
public Sprite dice5;
public Sprite dice6;
public Button btnRollDice;
private float stepX;
private int speed = 3;
// Start is called before the first frame update
void Start()
{
stepX = 0;
}
// Update is called once per frame
void Update()
{
Vector3 target = character.transform.position;
target.x = stepX;
character.transform.position = Vector3.MoveTowards(character.transform.position, target, Time.deltaTime * speed);
if(character.transform.position == target)
{
btnRollDice.GetComponent<Image>().sprite = dice0;
}

}
public void RollDice()
{
int result = Random.Range(1, 6);
Debug.Log("Dice rolled: " + result);
stepX -= 1.1f * result;
// Display sprite graphics
switch(result)
{
case 1:
btnRollDice.GetComponent<Image>().sprite = dice1;
break;
case 2:
btnRollDice.GetComponent<Image>().sprite = dice2;
break;
case 3:
btnRollDice.GetComponent<Image>().sprite = dice3;
break;
case 4:
btnRollDice.GetComponent<Image>().sprite = dice4;
break;
case 5:
btnRollDice.GetComponent<Image>().sprite = dice5;
break;
case 6:
btnRollDice.GetComponent<Image>().sprite = dice6;
break;
default:
btnRollDice.GetComponent<Image>().sprite = dice0;
break;
}

}
}
Fill in the blanks :)
Button image will change accordingly.

Completed Project

--

--

--

I write stuffs. Novels, programs, mobile apps, journal papers, etc.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Kerberos Configuration with Windows Active Directory

Corda 5 “The road ahead” — Part 3 — Introduction to the Architecture

Causes of scope creep and what to do about them

Thoughts on Releasing Something into the World

Hacking with Inspect element 😁

Recent work plan of Team PowerJob

Move between View Controllers with Segues — iOS #9

From mobile to web development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Raptor Kwok

Raptor Kwok

I write stuffs. Novels, programs, mobile apps, journal papers, etc.

More from Medium

Post Processing in unity

A Quick Guide to Using Coroutines with Unity!

How To Use Post Processing In Unity

2D Mobile Game: Player Health System