using UnityEngine; using System.Collections; public class SlotMachineScriptableObject : MonoBehaviour { public ReelScriptableObject[] reels; // Array of reels used in the slot machine public float faceSpriteSize = 1.0f; // Size of the face sprites public float spacing = 0.5f; // Spacing between the face sprites public float reelSpacing = 2.0f; // Spacing between the reels public float spinSpeed = 5.0f; // Speed at which the faces move private void Start() { BuildSlotMachine(); } private void BuildSlotMachine() { // Build the slot machine by setting up the reels and faces for (int i = 0; i < reels.Length; i++) { // Create a GameObject for each reel GameObject reelObj = new GameObject("Reel" + (i + 1)); reelObj.transform.SetParent(transform); // Set the initial position of the reel Vector3 initialPosition = new Vector3(i * reelSpacing, 0, 0); // Adjust this as needed reelObj.transform.localPosition = initialPosition; // Set the reel sprite SpriteRenderer reelRenderer = reelObj.AddComponent(); reelRenderer.sprite = reels[i].sprite; reelRenderer.sortingLayerName = "Default"; // Adjust as needed reelRenderer.sortingOrder = i; // Debug logs if (reels[i].sprite != null) { Debug.Log("Reel Sprite Assigned: " + reels[i].sprite.name); } else { Debug.LogError("Reel Sprite is null for Reel" + (i + 1)); } // Create and position face objects for (int j = 0; j < reels[i].faceSlots; j++) { GameObject faceObj = new GameObject("Face" + (j + 1)); faceObj.transform.SetParent(reelObj.transform); faceObj.transform.localPosition = new Vector3(0, j * (faceSpriteSize + spacing), 0); // Start above the reel // Set the face sprite SpriteRenderer faceRenderer = faceObj.AddComponent(); faceRenderer.sprite = reels[i].faces[j].sprite; faceRenderer.transform.localScale = new Vector3(faceSpriteSize, faceSpriteSize, 1); faceRenderer.sortingLayerName = "Default"; // Adjust as needed faceRenderer.sortingOrder = i * 10 + j; faceRenderer.enabled = false; // Initially disable rendering } } } public void StartSpin() { StartCoroutine(SpinReels()); } private IEnumerator SpinReels() { for (int i = 0; i < reels.Length; i++) { StartCoroutine(SpinReel(reels[i], i)); yield return new WaitForSeconds(0.5f); } yield return new WaitForSeconds(1.0f); IdentifyWinningFace(); } private IEnumerator SpinReel(ReelScriptableObject reel, int reelIndex) { float spinDuration = 2.0f; // Duration of the spin float elapsed = 0.0f; // Get the reel GameObject GameObject reelObj = transform.GetChild(reelIndex).gameObject; while (elapsed < spinDuration) { elapsed += Time.deltaTime; // Move face objects down the reel for (int j = 0; j < reel.faceSlots; j++) { Transform faceTransform = reelObj.transform.GetChild(j); SpriteRenderer faceRenderer = faceTransform.GetComponent(); // Move the face sprite down faceTransform.localPosition -= new Vector3(0, spinSpeed * Time.deltaTime, 0); // Show the face sprite only when within the reel bounds if (faceTransform.localPosition.y < 0 && faceTransform.localPosition.y > -(reel.faceSlots - 1) * (faceSpriteSize + spacing)) { faceRenderer.enabled = true; } else { faceRenderer.enabled = false; } // Wrap around to the top if the face moves out of view if (faceTransform.localPosition.y < -((reel.faceSlots - 1) * (faceSpriteSize + spacing))) { faceTransform.localPosition = new Vector3(0, (reel.faceSlots - 1) * (faceSpriteSize + spacing), 0); } } yield return null; } // Stop the spin and identify the winning face int winningIndex = Random.Range(0, reel.faceSlots); GameObject winningFaceObj = reelObj.transform.GetChild(winningIndex).gameObject; Debug.Log("Winning Face: " + reel.faces[winningIndex].itemName); } private void IdentifyWinningFace() { Debug.Log("Identifying the winning face..."); } }