Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by xandermacleod · Feb 11, 2014 at 04:31 PM · listdividedivision

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);
         }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xandermacleod · Feb 11, 2014 at 04:37 PM 0
Share

You sir, are a champion amongst mere mortals! That worked perfectly. I would've never worked that out. Thanks loads!

avatar image whydoidoit · Feb 11, 2014 at 04:39 PM 0
Share

No problemo :) Switched it to an answer - would you $$anonymous$$d ticking it?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Find the differences between two lists 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges