Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created May 6, 2024 12:04
Show Gist options
  • Save unitycoder/ff0aac1089711bf5e7f0dd742befeee9 to your computer and use it in GitHub Desktop.
Save unitycoder/ff0aac1089711bf5e7f0dd742befeee9 to your computer and use it in GitHub Desktop.
align cards on circle edge
// https://discussions.unity.com/t/need-help-with-positioning-while-object-is-rotated/344542/2
private void SetCardPosition(int index)
{
//The angle is based on how far the card is from the midpoint of the hand.
//Note that the midpoint will either be a whole number or x.5
float midpoint = (cards.Count-1) / 2f;
float angle = angleDelta * (midpoint - index);
//Positive angles rotate counterclockwise, negative angles rotate clockwise
cards[index].transform.eulerAngles = new Vector3(0, 0, angle);
//Mathf uses radians
//A card that is rotated counterclockwise is on the left side of the hand,
//while a card rotated clockwise should be on the right side of the hand.
//This means we need to flip either the angle or the x value when calculating the
//position.
angle *= -Mathf.Deg2Rad;
float x = Mathf.Sin(angle) * radius;
float y = Mathf.Cos(angle) * radius;
cards[index].transform.position = new Vector3(center.x + x, center.y + y, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment