我正在尝试制作老虎机原型。 我的目标是模拟(编程)符号运动。我希望我的符号向下移动,当每个符号离开游戏区域时,它应该再次移动到框架的顶部。这是我的代码:
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);
}
}
}
}
}
我的符号是常规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 () {
}
}
所以,我的符号移动但是在最低符号再次上升之后,符号之间的差距就不一样了。
如果有人有制作老虎机的经验,请与我分享。 谢谢,祝你有个愉快的一天
答案 0 :(得分:3)
你的问题在这里:
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);
}
您正在检查符号是否超过检查点,因此我们假设transform.position.y
等于-3.1f
,然后您尝试通过添加3.0f
重置它( symbolHeight
)而不是实际的位置变化(3.1f
)。这只是一个例子,当然价值观可能会有所不同。
为了解决这个问题,我建议你这样做:
if (symbol.transform.position.y <= -3.0f)
{
Vector3 currentPosition = symbol.transform.position;
currentPosition.y = (currentPosition.y + 3.0f) + (symbols.Count - 1) * symbolHeight;
symbol.transform.position = currentPosition;
}