我試圖製作一個老虎機原型。 我的目標是模擬(以編程方式)符號移動。我希望我的符號向下移動,當每個符號離開遊戲場時,它應該再次移動到幀的頂部。這裏是我的代碼:團結無限循環的移動的精靈(老虎機卷軸)
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);
}
}
}
}
}
我的符號是一個普通的遊戲對象:
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() {
}
}
所以,我的符號移動,但最低的符號再次上升後,符號之間的差距是不一樣的。
如果有人在做老虎機的經驗,請與我分享。 感謝,並有一個愉快的一天
在第一次看你的問題似乎很多的迭代之後,因爲涉及到'Translate'功能你肯定會失去一些準確的浮點不準確 - 另一方面,它看起來有點太極端,不能成爲唯一的解釋,所以我不能100%確定,我會盡力重現這一點。 –
你計算'newY',但你不使用它。您仍然將y設置爲'symbols.Count * symbolHeight'。可能就是這樣。 – Pluto