I am trying to do this for couple of days…and the best i got is this
and when hover
I do not like it…looks bad and feels bad.
I want something like this
My attempt
public void ArrangeCardsHover(int cardIndex)
{
List<GameObject> cards = new List<GameObject>();
foreach (Transform child in handCenter)
{
cards.Add(child.gameObject);
}
int cardCount = cards.Count;
// Calculate the total width occupied by spaces between cards
float totalWidth = (cardCount - 1) * spacing;
// Calculate the starting position for the first card to be centered horizontally
float startXPos = handCenter.position.x - (totalWidth / 2f);
for (int i = 0; i < cardCount; i++)
{
float extraSpacing = 0f;
if (i > cardIndex && cardIndex != -1)
{
extraSpacing = extraSpacingAfterIndex;
}
// Update x position based on index, spacing, and extraSpacing
float xPos = startXPos + (i * spacing) + extraSpacing;
// Keep the y position the same as handCenter
float yPos = handCenter.position.y;
// Set the position
cards[i].transform.position = new Vector3(xPos, yPos, 0f);
//stop any tween except the index
if (cardIndex != i)
{
LeanTween.cancel(cards[i]);
//LeanTween.cancel(cards[i].transform.GetChild(0).gameObject);
}
}
}
And on the hover i just use this script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class CardEvents : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
private float hoverScale = 1.2f;
private float transitionTime = 0.2f;
private float hoverHeight = 100f;
private Vector3 originalScale;
private int index = -1; // Initialize the index to -1
private GameObject childObjectVisual;
public LTDescr scaleTween;
public LTDescr moveTween;
void Start()
{
originalScale = transform.localScale;
childObjectVisual = gameObject.transform.GetChild(0).gameObject;
//originalPos = transform.position;
}
public void OnPointerEnter(PointerEventData eventData)
{
// Cancel any ongoing tweens
//LeanTween.cancel(childObjectVisual);
//DeckManager.Instance.ArrangeCardsHover(-1);
// Scale up the hovered card
scaleTween = LeanTween.scale(childObjectVisual, originalScale * hoverScale, transitionTime);
// Move the card slightly up in world space
float targetY = transform.position.y + hoverHeight;
moveTween = LeanTween.moveY(gameObject, targetY, transitionTime);
index = FindCardIndex(childObjectVisual);
DeckManager.Instance.ArrangeCardsHover(index);
}
public void OnPointerExit(PointerEventData eventData)
{
index = -1; // Reset the index when the pointer exits the card
//DeckManager.Instance.ArrangeCardsHover(-1);
//LeanTween.cancel(childObjectVisual); // Cancel any ongoing tweens
// Scale down the card
scaleTween = LeanTween.scale(childObjectVisual, originalScale, transitionTime);
////// Move the card slightly down in world space
//float targetY = transform.position.y - hoverHeight;
//LeanTween.moveY(gameObject, targetY, transitionTime);
DeckManager.Instance.ArrangeCardsHover(-1);
}
private int FindCardIndex(GameObject card)
{
List<GameObject> cards = new List<GameObject>();
foreach (Transform child in DeckManager.Instance.handCenter)
{
cards.Add(child.GetChild(0).gameObject);
}
// Iterate through the list of cards to find the index of the specified card
for (int i = 0; i < cards.Count; i++)
{
if (cards[i] == card)
{
return i; // Return the index if the card is found
}
}
return -1; // Return -1 if the card is not found
}
}