I’m somewhat new at C# programming.
I’m making a slot machine and the issue I am facing comes down to transform.position.y
. I have 3 horizontal image strips for each reel. Each symbol is exactly 200px apart in Photoshop and when I manually type in a value of 2 (see below) into the inspector the symbols line up as expected. The symbols are mapped out like so in a method I use to randomize them on startup so the machine has different symbols every time the player starts a game, and that works fine – it shows each symbol where it is supposed to be, flawlessly and every time. The symbols are mapped as follows:
//Orange
transform.position = new Vector2(transform.position.x, 0f);
transform.position = new Vector2(transform.position.x, transform.position.y - 22);
//Pear
transform.position = new Vector2(transform.position.x, 0f);
transform.position = new Vector2(transform.position.x, transform.position.y - 20);
//Plumb
transform.position = new Vector2(transform.position.x, 0f);
transform.position = new Vector2(transform.position.x, transform.position.y - 18);
and etc down the reel.
When it comes time for the spin, the following loop is executed in order to transform the reels downwards by 2 each time to line up each symbol where it’s supposed to be, and at first it works, however sometimes the symbols are not exactly in place and they are either up or down by a few units and it’s noticeable.
for (int i = 0; i < 50; i++){
//Do other things that do not relate to my problem
//If the spinning reel moves out of bounds, snap it back to position - works
if (transform.position.y <= -22)
transform.position = new Vector2(transform.position.x, +22);
//Does not work as intended
//Transform.pos.x is not 0 so it stays within reel bounds, it works.
transform.position = new Vector2(transform.position.x, transform.position.y - 2);
//Do other things that do not relate to my problem
}
I have tried a few different things to fix this, and none of them work, the same thing always happens, after a few spins one or two symbols will not line up as it should. I have tried using MathF.Floor
, MathF.Ceil
(which had very bad results) and MathF.Round
– and I have also cast all of these methods ToInt
because I’m working with a single number.
What can I do? I think I’m doing the right thing, but I could be wrong.
Thanks!
2
Answers
It turns out in my case that the sprites I have been using were not aligned properly, which is very embarrassing but that's how we learn, right? Altering the sprites just a little here and there leads to expected results, which is baffling due to how they are laid out. Oh well, these things happen I guess. I'll leave this up in case anyone runs into something similar in future.
My advice to anyone else in this sort of situation is to open your seperate sprites and overlay them on top of eachother. Glaring mistakes will pop out immediately. No math or rounding needed.
I am not sure it will help, but you may need to use the
Time.DeltaTime
https://docs.unity3d.com/ScriptReference/Time-deltaTime.html, to make certain things are firing on frame?Also you might find better guidance here: https://gamedev.stackexchange.com/