Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 DJGhostViper · Dec 17, 2016 at 10:36 PM · resourcesintrtsincreasedecrease

Increasing or decreasing int values quickly?

I'm making an RTS game where resources are gathered and taken away, when gaining or spending resources I would like to increase or decrease them quickly instead of taking them or adding them all at one shot. The resources are int values since I don't want decimals in my resources. Is there an easy way to do this? Thanks!!

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 Pengocat · Dec 18, 2016 at 12:22 AM

I put this together that try to go for the resourcesTarget by lerping on a curve that slows down when close to the target value. Just for previewing the resourcesTarget is Serialized so it can be changed in the inspector but it should work better if set using the ResourcesTarget Property. You can also play with the lerpDelay to control the time to reach the target.

     [Range(0, 1000000)]
     [SerializeField]
     private int resourcesTarget = 100000;
     [Range(0.1f, 20f)]
     public float lerpDelay = 2f;
     public int resourcesDisplayed;
     Text resourcesTextBox;
     string resourcesText;
     bool isTargetChanged;
     public int ResourcesTarget
     {
         get
         {
             return resourcesTarget;
         }
         set
         {
             resourcesTarget = value;
             isTargetChanged = true;
         }
     }
     void Start()
     {
         StartCoroutine(ResourcesCorrection());
         resourcesTextBox = GetComponent<Text>();
     }
 
     void Update()
     {
         if (resourcesTextBox.text != resourcesText)
         {
             resourcesTextBox.text = resourcesText;
         }
     }
 
     IEnumerator ResourcesCorrection()
     {
         while (true)
         {
             if (resourcesTarget == resourcesDisplayed)
             {
                 yield return null;
             }
             else
             {
                 int resourcesOld = resourcesDisplayed;
                 isTargetChanged = false;
                 float currentLerpTime = 0f;
                 while (resourcesTarget != resourcesDisplayed && isTargetChanged == false)
                 {
                     currentLerpTime += Time.deltaTime;
                     if (currentLerpTime > lerpDelay)
                     {
                         currentLerpTime = lerpDelay;
                     }
                     float t = currentLerpTime / lerpDelay;
                     t = Mathf.Sin(t * Mathf.PI * 0.5f);
                     resourcesDisplayed = (int)Mathf.Lerp(resourcesOld, resourcesTarget, t);
                     resourcesText = resourcesDisplayed.ToString();
                     yield return null;
                 }
             }
         }
     }
Comment
Add comment · Show 7 · 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 DJGhostViper · Dec 18, 2016 at 10:53 PM 0
Share

This works great thanks!! Quick question though, im think about putting this on every building I can place also so that I can change the lerp time to the amount of time the building takes to build so that it takes resources away while its building would the above script work well with that too?

@Pengocat

avatar image Pengocat DJGhostViper · Dec 18, 2016 at 11:10 PM 0
Share

Yes it could work. You could lerp the cost per building separately. So if the building cost 2000 you lerp from 0-2000 and the lerpDelay = buildtime. $$anonymous$$eanwhile you also subtract the build cost from the total resources pool. You could remove the $$anonymous$$athf.Sin line to get a linear curve.

avatar image DJGhostViper · Dec 18, 2016 at 11:04 PM 0
Share

If the LerpTime (Lerpdelay) equals the buildingTime of the building the resources being taken away lines up with the building completing its build perfectly. I don't want have to keep changing the Lerptime to the buildingTime so I can have the building complete its build and the resources for that building to stop being taken away at the same time. Any Solution to this?

avatar image DJGhostViper · Dec 19, 2016 at 01:19 AM 0
Share

Wouldnt this be difficult when multiple buildings are building because then the lerptime will be jumping all around depending on different build times @Pengocat

avatar image Pengocat DJGhostViper · Dec 19, 2016 at 01:59 AM 0
Share

You could run multiple coroutines with different payAmount and buildTime.

     public int totalResources = 10000;
 
     void Update()
     {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
         {
             StartCoroutine(PayResourcesDuringBuild(2000, 5f));
         }
     }
 
     IEnumerator PayResourcesDuringBuild(int payAmount, float buildTime)
     {
         int currentAmount = 0;
         int subtract = 0;
         float currentLerpTime = 0f;
         int alreadySubtracted = 0;
         while (payAmount != currentAmount)
         {
             currentLerpTime += Time.deltaTime;
             if (currentLerpTime > buildTime)
             {
                 currentLerpTime = buildTime;
             }
             float t = currentLerpTime / buildTime;
             currentAmount = (int)$$anonymous$$athf.Lerp(0, payAmount, t);
             subtract = currentAmount - alreadySubtracted;
             totalResources -= subtract;
             alreadySubtracted += subtract;
             yield return null;
         }
     }
avatar image DJGhostViper · Dec 19, 2016 at 09:19 PM 0
Share

Thank you so much for your help!

avatar image Pengocat DJGhostViper · Dec 19, 2016 at 10:35 PM 0
Share

You are welcome. Good luck with the game.

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

61 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

How can you make an AI perform specific movements? 2 Answers

Increase/Decrease Over Time, Not Frames 1 Answer

Increasing the speed for a few seconds doesn't work. 1 Answer

controlling the increment in the variable 1 Answer

RTS Style building help 2 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