Hi all, In 2016 I’ve tried Unity but terribly failed. Now I try again. I’m trying to make a game and want to use 3 dice. It looks like a slot machine. I’m now working only on the first die because if I can make that die “roll” perfectly, I probably can also make the other two dice roll. These are the functions that all work:
public class rolls : MonoBehaviour{
float speed = 6.0f;
public bool startButtonPressed = false;
public bool willStopRoll1 = false;
public bool roll1Stopped = false;
public int number1 = 0;
// Custom Functions \\
void SpinRoll (){
roll1Stopped = false;
if (transform.position.y <= -7.45f){
transform.position = new Vector3 (0, 0, 0);
}
else{
transform.Translate (0, -speed * Time.deltaTime, 0);
}
}
void CreateRandomNumber1 (){
number1 = Random.Range(1, 7);
print(number1);
}
void StopRoll1 (){
if (number1 == 1){
if (transform.position.y <= -1.2f && transform.position.y >= -1.3f){
transform.position = new Vector3 (0, -1.25f, 0);
roll1Stopped = true;
}
else{
SpinRoll ();
}
}
if (number1 == 2){
if (transform.position.y <= -3.7f && transform.position.y >= -3.8f){
transform.position = new Vector3 (0, -3.75f, 0);
roll1Stopped = true;
}
else{
SpinRoll ();
}
}
if (number1 == 3){
if (transform.position.y <= -6.2f && transform.position.y >= -6.3f){
transform.position = new Vector3 (0, -6.25f, 0);
roll1Stopped = true;
}
else{
SpinRoll ();
}
}
if (number1 == 4){
if (transform.position.y <= -2.45f && transform.position.y >= -2.55f){
transform.position = new Vector3 (0, -2.5f, 0);
roll1Stopped = true;
}
else{
SpinRoll ();
}
}
if (number1 == 5){
if (transform.position.y <= -4.95f && transform.position.y >= -5.05f){
transform.position = new Vector3 (0, -5f, 0);
roll1Stopped = true;
}
else{
SpinRoll ();
}
}
if (number1 == 6){
if (transform.position.y <= 0 && transform.position.y >= -0.05f){
transform.position = new Vector3 (0, 0, 0);
roll1Stopped = true;
}
else{
SpinRoll ();
}
}
}
But now comes the part I can’t seem to make happen. I made a UI button and linked it to my public void RollTheDice(); In this function I want the following to happen:
-
SpinRoll (); for a couple of seconds.
-
CreateRandomNumber1 ();
-
StopRoll1 (); at the randomly chosen number.
-
If roll 1 has stopped (roll1Stopped == true) it’s time to stop roll 2.
I’ve tried a lot of things but every time Unity crashes or roll 1 is only spinning for a fraction of a second. I didn’t save every try but this is what I’ve still got in my script.
public void RollTheDice (){
int spinsBeforeRollStops = 6;
for(int i = 0; i < spinsBeforeRollStops; i++){
SpinRoll ();
}
CreateRandomNumber1 ();
//Invoke("StopRoll1", 2.0f);
StopRoll1 ();
if (roll1Stopped == true){
print("Time to stop roll 2!");
}
}
So how will I be able to combine all my functions into the RollTheDice function so it looks like a slot machine roll is spinning and stopping at the right moment?
Thank you in advance.
I don’t see anything there about crashing. Maybe that was in some other code.
However, there is no real “time delay” in your functions.
If this is your beginning in Unity, I would highly recommend that you try some easier content, and work your way into being comfortable (and practiced) in Unity and scripting together. I believe that will make you less frustrated, and less likely to quit… as well as happier to succeed.
Sincerely.
All of your calculations happen within the frame, even if they take a little bit of computer time, there is no time for the changes to be visible, as I believe you imagine it should be working, if that makes sense.
1 Like
You’re right. It doesn’t crash anymore. It was crashing when I was using a while loop.
I understand what you are saying. Learn more before you try to make this game. And I do follow a video course but I also want to create my own stuff too.
I can make it happen (sort of) using the Update function and the Inspector. You can see what I mean below.
Yep, it’s not that I don’t think it’s a good idea to make your own stuff… obviously 
I just mean that you might like the knowledge that comes from learning the building blocks (that’s the best way I can describe it), because you will use these so often in your game.
Let’s say your code looked like this:
1 frame: do a number of turns, adjusting position, etc…
(end of the frame, your result is now displayed in the game)
actual desired game output is:
begin action
want it to last (i don’t know, 3 seconds)
updates position gradually every frame
… etc
One way would be to use a coroutine for spinning.
Another option is to use the Update() loop with some bool variables for “spinning” or something like that, perhaps a counter for the duration, that increments by Time.deltaTime
Something like:
bool spinning = false;
float spinTimer = 0;
void Update() {
if(!spinning && Input.GetKeyDown(KeyCode.S)) {
spinning = true;
spinTimer = 0;
}
if(spinning)
{
if (transform.position.y <= -7.45f)
{
transform.position = new Vector3 (0, 0, 0);
}
else transform.Translate (0, -speed * Time.deltaTime, 0);
spinTimer += Time.deltaTime;
if(spinTimer >= 3)
{
spinning = false;
}
}
}
Something like that?
1 Like
Yes great, thank you. Funny how I have to think completely different than I thought I should. I managed to implement your code and add some more so there is also a random number and it also stops at the right number on the die.
Of course, there are new questions. How come the spinning isn’t at a consistent speed? (See video below) Can it be my old iMac I’m working on or has it something to do with the code?
I really can’t be sure watching that video of what you’re asking. That’s probably my fault, though, not yours 
I’m glad it’s working at least somewhat, and you felt comfortable adding to it. If you’re working on it, and think something isn’t right, though (and can’t work it out), I’d suggest that you post your updated code for readers to look at. 
1 Like
You’re right again
This is my code so far.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rolls : MonoBehaviour{
float speed = 6.0f;
public bool startButtonPressed = false;
public bool roll1StoppedSpinning = false;
public int number1 = 0;
public bool spinning = false;
public bool noNumberYet = true;
float spinTimer = 0;
// Use this for initialization \\
void Start (){
}
// Update is called once per frame \\
void Update (){
if (!spinning && Input.GetKeyDown(KeyCode.S)){
noNumberYet = true;
spinning = true;
spinTimer = 0;
}
if (spinning){
if (transform.position.y <= -7.45f){
transform.position = new Vector3 (0, 0, 0);
}
else
transform.Translate (0, -speed * Time.deltaTime, 0);
spinTimer += Time.deltaTime;
if (spinTimer >= 1){
roll1StoppedSpinning = true;
spinning = false;
}
}
if (roll1StoppedSpinning && noNumberYet){
CreateRandomNumber1 ();
noNumberYet = false;
}
else StopRoll1 ();
}
// Custom Functions \\
void SpinRoll (){
roll1StoppedSpinning = false;
if (transform.position.y <= -7.45f){
transform.position = new Vector3 (0, 0, 0);
}
else{
transform.Translate (0, -speed * Time.deltaTime, 0);
}
}
void CreateRandomNumber1 (){
number1 = Random.Range(1, 7);
print(number1);
}
void StopRoll1 (){
if (number1 == 1){
if (transform.position.y <= -1.2f && transform.position.y >= -1.3f){
transform.position = new Vector3 (0, -1.25f, 0);
roll1StoppedSpinning = true;
}
else{
SpinRoll ();
}
}
if (number1 == 2){
if (transform.position.y <= -3.7f && transform.position.y >= -3.8f){
transform.position = new Vector3 (0, -3.75f, 0);
roll1StoppedSpinning = true;
}
else{
SpinRoll ();
}
}
if (number1 == 3){
if (transform.position.y <= -6.2f && transform.position.y >= -6.3f){
transform.position = new Vector3 (0, -6.25f, 0);
roll1StoppedSpinning = true;
}
else{
SpinRoll ();
}
}
if (number1 == 4){
if (transform.position.y <= -2.45f && transform.position.y >= -2.55f){
transform.position = new Vector3 (0, -2.5f, 0);
roll1StoppedSpinning = true;
}
else{
SpinRoll ();
}
}
if (number1 == 5){
if (transform.position.y <= -4.95f && transform.position.y >= -5.05f){
transform.position = new Vector3 (0, -5f, 0);
roll1StoppedSpinning = true;
}
else{
SpinRoll ();
}
}
if (number1 == 6){
if (transform.position.y <= 0 && transform.position.y >= -0.05f){
transform.position = new Vector3 (0, 0, 0);
roll1StoppedSpinning = true;
}
else{
SpinRoll ();
}
}
}
}