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 poofdragon · Aug 22, 2012 at 04:12 AM · c#objectrotate

Turning Spinners Based on an Integer

Here's the situation:

I'm trying to create a number spinner utilizing 4 identical cylindrical GameObjects as each of the individual digits making up a four digit number (ex. |1|2|3|4|, |9|9|9|9|). I have much of the logic in place, I think, but I've reached a point where I'm stuck. I am able to rotate the Spinners at random and reset them to their Origin position without a problem.

The last step of getting the spinners to increment properly as the values change has me stumped. At this point every time I change the TargetValue Unity stops responding and I have to force close. No errors or anything, so it has to be something I'm missing in the setSpinner() logic, but what? I don't know.

Help?

update:

I have managed to get the spinners working for setting each digit up by one step. In the process I gutted the original code, but I still have issues dealing with the hand off between the "ones" and "tens" places and so forth.

For instance if I set the variable for TargetValue = 100 the result is tensCount = 9 and onesCount = 1, leaving the spinners at |0|0|9|1|.

Any help on sorting out my logic here would be a great help, thanks ^^

update #2:

Alright! I managed to get everything working with the code below! All spinners work as expected. The only remaining problem is that I don't really know how to go about inserting a mechanism that would add a bit of fluidity to the movement of the numbers so that they count at a variable rate.

For example:

A large jump in the DifferenceValue would result in faster spinning, while a small change would result in a slow rotation. All of which is calculated from the "base speed" SpinSpeed.

Any extra number magic that might allow for "over roll" or "under roll" and correction as appropriate would be much appreciated.


edit: revised code

 using UnityEngine;
 using System.Collections;
 
 public class SpinnerManager : MonoBehaviour {
     
     public static SpinnerManager Instance;
     
     public bool InTestMode;
     public bool ResetSpinner;
     
     public bool IsSpinning;
     
     public float SpinSpeed = 1.0f;
     
     public int TargetValue;
     public int CurrentValue;
     public int DifferenceValue;
     
     // spinner wheels
     public GameObject OnesPlace;
     public GameObject TensPlace;
     public GameObject HundredsPlace;
     public GameObject ThousandsPlace;
     
     // reset positions
     private Transform onesPlaceOrigin;
     private Transform tensPlaceOrigin;
     private Transform hundredsPlaceOrigin;
     private Transform thousandsPlaceOrigin;
     
     public int countDirection = 0;
     public int onesCount = -1;
     public int tensCount = -1;
     public int hundredsCount = -1;
     public int thousandsCount = -1;    
     
     
     // Use this for initialization
     void Awake ()
     {
         Instance = this;    
         
         // get reset postions
         onesPlaceOrigin = OnesPlace.transform;
         tensPlaceOrigin = TensPlace.transform;
         hundredsPlaceOrigin = HundredsPlace.transform;
         thousandsPlaceOrigin = ThousandsPlace.transform;
         
         resetSpinner();        
     }
     
     // Update is called once per frame
     void Update ()
     {
         // find the difference in values
         DifferenceValue = TargetValue - CurrentValue;
         
         if (DifferenceValue < 0){countDirection = -1;}
         if (DifferenceValue > 0){countDirection = 1;}
         
         if(InTestMode)
         {        
             // test spinners
 //            OnesPlace.transform.Rotate(0,0,Time.deltaTime * -SpinSpeed);
 //            TensPlace.transform.Rotate(0,0,Time.deltaTime * SpinSpeed);
 //            HundredsPlace.transform.Rotate(0,0,Time.deltaTime * -SpinSpeed);
 //            ThousandsPlace.transform.Rotate(0,0,Time.deltaTime * SpinSpeed);
 //            IsSpinning = true;
             setSpinner();
             TargetValue = Random.Range(0,9999);
             return;
         }
          if(DifferenceValue != 0 && !InTestMode)
         {
             // if CurrentValue != TargetValue, Spin until values match.
             setSpinner();
             return;
         }
         else
         {
             IsSpinning = false;
             if(ResetSpinner)
             {
                 resetSpinner();
                 return;
             }
         }
     }
     
     void resetSpinner()
     {
         float rotateSpeed = Time.deltaTime * (SpinSpeed * 100);
         OnesPlace.transform.Rotate(0,0,onesPlaceOrigin.rotation.z * rotateSpeed);
         TensPlace.transform.Rotate(0,0,tensPlaceOrigin.rotation.z * rotateSpeed);
         HundredsPlace.transform.Rotate(0,0,hundredsPlaceOrigin.rotation.z * rotateSpeed);
         ThousandsPlace.transform.Rotate(0,0,thousandsPlaceOrigin.rotation.z * rotateSpeed);
         countDirection = 0;
         onesCount = 0;
         tensCount = 0;
         hundredsCount = 0;
         thousandsCount = 0;
         TargetValue = 0;
         CurrentValue = 0;
         DifferenceValue = 0;
         
     }
     
     void oneStepSpin(GameObject whichSpinner)
     {
         if(whichSpinner != null)
         {            
             //set rotation speed and rotate 36degrees for each step
             //??? this doesn't look exactly right...
             float rotateSpeed = Time.deltaTime * SpinSpeed;
             float rotateDifference = -countDirection * 36;
             
             whichSpinner.transform.Rotate(0,0,rotateDifference); //rotateSpeed *
         }
     }
     
     void setSpinner()
     {
         
         // While the DifferenceValue isn't 0 and the CurrentValue is between 0-9999
         if(CurrentValue != TargetValue && (CurrentValue <= 9999 || CurrentValue >= 0))
         {    
             IsSpinning = true;
             
             //start counting                        
             onesCount += countDirection;
             CurrentValue += countDirection;
             Debug.Log("count = " + CurrentValue);
             
             //count up
             if(onesCount > 9 && countDirection > 0 && CurrentValue < 9999)
             {
                 oneStepSpin(TensPlace);
                 tensCount++;
                 onesCount = 0;
                 
                 if(tensCount > 9)
                 {
                     oneStepSpin(HundredsPlace);
                     hundredsCount++;
                     tensCount = 0;
                     
                     if(hundredsCount > 9)
                     {
                         oneStepSpin(ThousandsPlace);
                         thousandsCount++;
                         hundredsCount = 0;
                     }
                 }
             }
             
             //count down
             if(onesCount < 0  && countDirection < 0 && CurrentValue > 10)
             {
                 oneStepSpin(TensPlace);
                 tensCount--;
                 onesCount = 9;
                 
                 if(tensCount < 0)
                 {
                     oneStepSpin(HundredsPlace);
                     hundredsCount--;
                     tensCount = 9;
                     
                     if(hundredsCount < 0)
                     {
                         oneStepSpin(ThousandsPlace);
                         thousandsCount--;
                         hundredsCount = 9;
                     }
                 }
             }
             // spin the OnesPlace
             oneStepSpin(OnesPlace);
         }
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

8 People are following this question.

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

Related Questions

Making a rigidbody cube snap 90 deg at a time 1 Answer

Unity 3d(Space) Questions 1 Answer

How to refer to objects, scripts, textures in Assets in C#? 0 Answers

Array empties values on RunTime? 1 Answer

Angry Birds Style Loader (Load the Birds to be fired) 0 Answers


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