- Home /
Turning Spinners Based on an Integer
Here's the situation:
I'm trying to create a number spinner utilizing 4 identical cylindrical GameObjects as each of the individual digits making up a four digit number (ex. |1|2|3|4|, |9|9|9|9|). I have much of the logic in place, I think, but I've reached a point where I'm stuck. I am able to rotate the Spinners at random and reset them to their Origin position without a problem.
The last step of getting the spinners to increment properly as the values change has me stumped. At this point every time I change the TargetValue Unity stops responding and I have to force close. No errors or anything, so it has to be something I'm missing in the setSpinner() logic, but what? I don't know.
Help?
update:
I have managed to get the spinners working for setting each digit up by one step. In the process I gutted the original code, but I still have issues dealing with the hand off between the "ones" and "tens" places and so forth.
For instance if I set the variable for TargetValue = 100 the result is tensCount = 9 and onesCount = 1, leaving the spinners at |0|0|9|1|.
Any help on sorting out my logic here would be a great help, thanks ^^
update #2:
Alright! I managed to get everything working with the code below! All spinners work as expected. The only remaining problem is that I don't really know how to go about inserting a mechanism that would add a bit of fluidity to the movement of the numbers so that they count at a variable rate.
For example:
A large jump in the DifferenceValue would result in faster spinning, while a small change would result in a slow rotation. All of which is calculated from the "base speed" SpinSpeed.
Any extra number magic that might allow for "over roll" or "under roll" and correction as appropriate would be much appreciated.
edit: revised code
using UnityEngine;
using System.Collections;
public class SpinnerManager : MonoBehaviour {
public static SpinnerManager Instance;
public bool InTestMode;
public bool ResetSpinner;
public bool IsSpinning;
public float SpinSpeed = 1.0f;
public int TargetValue;
public int CurrentValue;
public int DifferenceValue;
// spinner wheels
public GameObject OnesPlace;
public GameObject TensPlace;
public GameObject HundredsPlace;
public GameObject ThousandsPlace;
// reset positions
private Transform onesPlaceOrigin;
private Transform tensPlaceOrigin;
private Transform hundredsPlaceOrigin;
private Transform thousandsPlaceOrigin;
public int countDirection = 0;
public int onesCount = -1;
public int tensCount = -1;
public int hundredsCount = -1;
public int thousandsCount = -1;
// Use this for initialization
void Awake ()
{
Instance = this;
// get reset postions
onesPlaceOrigin = OnesPlace.transform;
tensPlaceOrigin = TensPlace.transform;
hundredsPlaceOrigin = HundredsPlace.transform;
thousandsPlaceOrigin = ThousandsPlace.transform;
resetSpinner();
}
// Update is called once per frame
void Update ()
{
// find the difference in values
DifferenceValue = TargetValue - CurrentValue;
if (DifferenceValue < 0){countDirection = -1;}
if (DifferenceValue > 0){countDirection = 1;}
if(InTestMode)
{
// test spinners
// OnesPlace.transform.Rotate(0,0,Time.deltaTime * -SpinSpeed);
// TensPlace.transform.Rotate(0,0,Time.deltaTime * SpinSpeed);
// HundredsPlace.transform.Rotate(0,0,Time.deltaTime * -SpinSpeed);
// ThousandsPlace.transform.Rotate(0,0,Time.deltaTime * SpinSpeed);
// IsSpinning = true;
setSpinner();
TargetValue = Random.Range(0,9999);
return;
}
if(DifferenceValue != 0 && !InTestMode)
{
// if CurrentValue != TargetValue, Spin until values match.
setSpinner();
return;
}
else
{
IsSpinning = false;
if(ResetSpinner)
{
resetSpinner();
return;
}
}
}
void resetSpinner()
{
float rotateSpeed = Time.deltaTime * (SpinSpeed * 100);
OnesPlace.transform.Rotate(0,0,onesPlaceOrigin.rotation.z * rotateSpeed);
TensPlace.transform.Rotate(0,0,tensPlaceOrigin.rotation.z * rotateSpeed);
HundredsPlace.transform.Rotate(0,0,hundredsPlaceOrigin.rotation.z * rotateSpeed);
ThousandsPlace.transform.Rotate(0,0,thousandsPlaceOrigin.rotation.z * rotateSpeed);
countDirection = 0;
onesCount = 0;
tensCount = 0;
hundredsCount = 0;
thousandsCount = 0;
TargetValue = 0;
CurrentValue = 0;
DifferenceValue = 0;
}
void oneStepSpin(GameObject whichSpinner)
{
if(whichSpinner != null)
{
//set rotation speed and rotate 36degrees for each step
//??? this doesn't look exactly right...
float rotateSpeed = Time.deltaTime * SpinSpeed;
float rotateDifference = -countDirection * 36;
whichSpinner.transform.Rotate(0,0,rotateDifference); //rotateSpeed *
}
}
void setSpinner()
{
// While the DifferenceValue isn't 0 and the CurrentValue is between 0-9999
if(CurrentValue != TargetValue && (CurrentValue <= 9999 || CurrentValue >= 0))
{
IsSpinning = true;
//start counting
onesCount += countDirection;
CurrentValue += countDirection;
Debug.Log("count = " + CurrentValue);
//count up
if(onesCount > 9 && countDirection > 0 && CurrentValue < 9999)
{
oneStepSpin(TensPlace);
tensCount++;
onesCount = 0;
if(tensCount > 9)
{
oneStepSpin(HundredsPlace);
hundredsCount++;
tensCount = 0;
if(hundredsCount > 9)
{
oneStepSpin(ThousandsPlace);
thousandsCount++;
hundredsCount = 0;
}
}
}
//count down
if(onesCount < 0 && countDirection < 0 && CurrentValue > 10)
{
oneStepSpin(TensPlace);
tensCount--;
onesCount = 9;
if(tensCount < 0)
{
oneStepSpin(HundredsPlace);
hundredsCount--;
tensCount = 9;
if(hundredsCount < 0)
{
oneStepSpin(ThousandsPlace);
thousandsCount--;
hundredsCount = 9;
}
}
}
// spin the OnesPlace
oneStepSpin(OnesPlace);
}
}
}