- Home /
List Trouble
Hi folks
I have a really weird problem on my hands. Im trying to do a very very basic division, which keeps going wrong and I was hoping someone could tell me where I've missed something.
Here's what I am trying to:
I'm creating a card game, and am trying to write a script to reposition the cards in a person's hand when they draw a new one. Because theoretically the player can have as many cards as they want in their hand, and those cards need to be readable, I have decided that every time a card gets added to the hand they should bunch together a little tighter, and then when the player mouses over a card, that card comes to the front. Currently though, the problem is strictly to do with the cards getting more closely bunched together.
To do this I have got two empty gameObjects to act as transform markers for the furthest points on either end of the hand a card could go. Then I have written a script to work out where each card should go between those two points. (for now ignore cards being on top of one another and just focus on their side-to-sideness). I have a list that contains each of my cards objects.
In the Debug Lines below it gets lines 15, 16 & 17 correct, but then lines 18 & 19 always goes funny. With 3 cards in the list, it usually creates a result of 0% for cards0&1, and then gives a value of 1(100%) for card2.
Any help with this basic division would be much appreciated. Thankyou.
public List<GameObject> HandCards; // holds instantiated card gameobjects
public Transform farLeftPoint; // the hand is resized dynamically as cards are added to the hand. This is the furthest left a card can be.
public Transform farRightPoint; // the hand is resized dynamically as cards are added to the hand. This is the furthest right a card can be.
public float spaceBetweenCards = 0.1f; // the amount of space thickness between each card
private int handSize; // how many cards are in the players hand?
void ResizeHand(int listNumber)
{
for (int t = 0; t < handSize; t++)
{
int temp = Mathf.Abs (listNumber + t); //calculate number of cards this card is from the focus card
float percent = (t+1) / handSize; //how far along in a 0-1 should this card be between the farLeft&farRight points
Debug.Log (t);
Debug.Log (t + 1);
Debug.Log (handSize);
Debug.Log ((t + 1)/handSize);
Debug.Log ("Card Number " + t + " is " + (percent * 100) + "% along the line.");
HandCards[t].transform.position = new Vector3(Mathf.Lerp(farLeftPoint.position.x, farRightPoint.position.x, percent), farLeftPoint.position.y - (temp * spaceBetweenCards), farLeftPoint.position.z);
}
}
Answer by whydoidoit · Feb 11, 2014 at 04:34 PM
I think you are running into an integer math issue - try this:
float percent = (float)(t+1) / (float)handSize;
You sir, are a champion amongst mere mortals! That worked perfectly. I would've never worked that out. Thanks loads!
No problemo :) Switched it to an answer - would you $$anonymous$$d ticking it?
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
A node in a childnode? 1 Answer
Find GameObjects with a certain Script [Solved] 1 Answer
print the whole list to screen 1 Answer