I'm trying to make a slot machine prototype. My goal is to simulate(programmatically) symbols movement. I want my symbols move down and when each symbol go out of game field, it should be moved to the frame's top again. Here is my code:
public class Reel : MonoBehaviour {
public int reelHeight = 3;
public int symbolWidth = 3;
public int symbolHeight = 3;
public float reelSpeed = 1000;
[SerializeField]
public GameObject symbol;
public List<GameObject> symbols = new List<GameObject>();
private bool inSpin = false;
void Start () {
// reel predefined symbols
int[] reelSymbols = new int[] { 0, 0, 0, 0, 1, 2, 1, 3, 4, 5}; //{0,0,0,0,1,1,1,2,2,3,3,3,4,4,4,4,1,5,5,5,5,2,2,6,6,6,6,3,7,7,7,7};
// instantiating symbols on reel
for (int i = 0; i < reelSymbols.Length; i++) {
symbol = Instantiate (symbol, new Vector3 (0, i*symbolHeight, 0), Quaternion.identity);
symbol.GetComponent<Symbol>().symbolType = reelSymbols [i];
symbols.Add (symbol);
}
}
public void SpinReel () {
inSpin = !inSpin;
}
void Update () {
// infinite spin
if (inSpin) {
foreach (GameObject symbol in symbols) {
// moving symbols down
float currentReelSpeed = reelSpeed * Time.deltaTime;
symbol.transform.Translate(Vector3.down * currentReelSpeed);
// move symbol up to the top if it's go down and no longer vidible
if (symbol.transform.position.y <= -3.0f) {
float newY = ((symbols.Count - 1) * symbolHeight) + transform.position.y;
symbol.transform.position = new Vector3 (transform.position.x, symbols.Count*symbolHeight, transform.position.z);
}
}
}
}
}
My symbol is a regular GameObject:
public class Symbol : MonoBehaviour {
public Sprite [] sArray;
public int symbolType;
// Use this for initialization
void Start () {
GetComponent<SpriteRenderer> ().sprite = sArray [symbolType];
}
// Update is called once per frame
void Update () {
}
}
So, my symbols move but after the lowest symbol goes up again, gap between symbols is not the same.
If anyone have experience in making slot machines, please share it with me. Thanks and have a nice day