It's nice to be here!
I've been scratching my head over an issue I am facing for days now, and i'm at my wit's end about how to get it working properly. I'm somewhat new at C# programming so forgive my noob behavior and code, ha.
I'm making a slot machine (isn't every third person?) 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 (correct me if my thinking is wrong, please.)
What can I do? I think i'm doing the right thing, but I could be wrong, so any help/input/advice is thoroughly appreciated.
Thanks!
CodePudding user response:
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?
// Note: untested as it is difficult to spin up new unity projects
for (int i = 0; i < 50; i )
{
if (transform.position.y <= -22)
{
transform.position = new Vector2(transform.position.x, 22 * Time.deltaTime);
}
transform.position = new Vector2(transform.position.x,
transform.position.y - 2 * Time.deltaTime);
}
Also you might find better guidance here: https://gamedev.stackexchange.com/
CodePudding user response:
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.