I am trying to make just a simple slot machine game. Ideally, it would be just like the screen of a slot machine at a casino. The code probably isn't the cleanest so I apologize for that.
I would like the objects to move along the y-axis until they hit a y-boundary. After hitting this y-boundary they will return to the top which would be "SpawnPosY". For some reason, I can't figure out. The objects drift away along the x-axis from the y-axis (The objects to the right of the y-axis keep moving further right, vice versa) every time it hits this y-boundary.
I tried to fix this by adding another portion of code that moves the icon back to the position on the x-axis but they keep drifting (Yes, I've removed this line of code and no luck)
I tracked one of the Icon's movements across the X-axis just in case it helps to have data. 1 will represent its position from the start and every data point after that will be the x-axis position after one reel rotation (the icon hitting the y-boundary).
0.369
-5.931
-18.533
-43.737
-94.145
-194.961
-396.593
Can anyone tell me why this is happening?
public class DestroyObjects : MonoBehaviour
{
public GameObject[] iconPrefabs;
public int iconNum = 0;
public float SpawnPosY = 12f;
private float SpawnPosX = 0.369f;
public float yBoundary = -0.369f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector2 spawnpos = new Vector2(transform.position.x, SpawnPosY);
Vector2 fixXpos = new Vector2(SpawnPosX, SpawnPosY);
if (transform.position.y < yBoundary)
{
transform.Translate(spawnpos);
transfrom.Translate(fixXpos);
}
}
}
You might be better off using a different overload of the Translate function that takes individual x, y, and z values so that you can specify to only move on the y axis.
You should also apply Time.deltaTime any time you're moving to normalize the framerate
var speed = 12f;
transform.Translate(0, speed * Time.deltaTime, 0);
Also, move the creation of the spawn position outside of the Update so that it is only calculated once, not every frame
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.