Skip to content

Instantly share code, notes, and snippets.

@ttruty
Created June 4, 2024 22:25
Show Gist options
  • Save ttruty/e2f4632754843787b417695bcd9159d5 to your computer and use it in GitHub Desktop.
Save ttruty/e2f4632754843787b417695bcd9159d5 to your computer and use it in GitHub Desktop.
Arrange pile of cards in Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CardManager : MonoBehaviour
{
[SerializeField] private GameObject cardPrefab;
[SerializeField] private int numberOfCards = 10;
[SerializeField] private float stackSpacing = 0.2f; // Adjust to control spacing between cards
[SerializeField] private Vector2 stackPosition = new Vector2(0, -3); // Bottom of the screen
private c
void Start()
{
// Set the CardManager's position to the center of the screen
for (int i = 0; i < numberOfCards; i++)
{
GameObject card = Instantiate(cardPrefab, transform);
float randomRotation = Random.Range(-15f, 15f);
// Position each card at the bottom third of the screen, slightly above the previous card
Vector3 cardPosition = new Vector3(transform.position.x, transform.position.y + i * stackSpacing, transform.position.z);
card.transform.position = cardPosition;
card.transform.rotation = Quaternion.Euler(0, 0, randomRotation);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment