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 OpenSky · Dec 09, 2016 at 04:43 AM · c#timemathgraphanimationcurve

Increment a variable by using an AnimationCurve graph

Hi.

I need some sort of a formula to increase an integer/float to 100 within a specified time, let's say 60 seconds.

I don't want it to increase linearly (add 10 every 6 seconds), but I want it to increase based on an AnimationCurve graph. This is very important.

This is my actual AnimationCurve graph alt text

So in this graph, the x represents the time and f(x) a value. For instance, the variable would increase a lot from 0.3 to 0.85, and only a little from 0 to 0.3 and from 0.85 to 1.

I've tried a lot of random formulas but it didn't go well.

Does anyone have an idea on how to do this ?


Edit 1 : I've managed to do something : what I've done is that I have a coroutine and its code is executed once every second. Then it calculates the percent of chance the value will be incremented (based on the curve so if the time is 0.3 it will have near 50% chances to be incremented). If not, add the value in a variable and retry the next second. If the next second the condition is true, it gets incremented by the sum.

The problem is that at the end the curve is going down, so the probability too. So the sum may never be added. So I've added to the code that if time > 1 then add the sum. It's still not what I want but at least it is something.

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class LikesManager : MonoBehaviour
 {
         public AnimationCurve curve;
     
         IEnumerator Start()
         {
             System.Random rnd = new System.Random();
             DateTime myDate = DateTime.Now;
     
             float totalTime = 60;
             int total = 100;
     
             float incrementingByThisSecond = total / totalTime;
             float varToBeIncremented = 0;
     
             while (true)
             {
                 yield return new WaitForSeconds(1f);
     
                 TimeSpan diff = DateTime.Now.Subtract(myDate);
     
                 if (diff.TotalSeconds <= totalTime + 1)
                 {
                     float secondsToGraph = (float)diff.TotalSeconds / totalTime;
     
                     if (rnd.Next(0, 100) < Mathf.RoundToInt(curve.Evaluate(secondsToGraph) * 100))
                     {
                         varToBeIncremented += incrementingByThisSecond;
                         incrementingByThisSecond = total / totalTime;
                         Debug.Log("Ok, new value = " + incrementingByThisSecond);
                     }
                     else
                     {
                         incrementingByThisSecond += total / totalTime;
                         Debug.Log("Not ok, new value = " + incrementingByThisSecond);
                     }
     
                     Debug.Log("Var = " + varToBeIncremented);
                 }
                 else
                 {
                     varToBeIncremented += incrementingByThisSecond;
                     incrementingByThisSecond = 0;
     
                     Debug.Log("The end, var =  " + varToBeIncremented);
                 }
             }
         }
 }

curve.png (15.5 kB)
Comment
Add comment · Show 1
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 HarshadK · Dec 09, 2016 at 05:52 AM 0
Share

You can use AnimationCurve.Evaluate with Time.time as parameter. Not sure if this is what you are looking for.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bieere · Dec 11, 2016 at 10:25 PM

When using Animationcurve.Evaluate() the value you evaluate is the position along the curve, relative to the x axis, which then returns the relative y value.

For example:

Animationcurve.Evaluate(0.5f) on your graph would return ~0.95f

Comment
Add comment · 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

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

3D Grapher - Function input problem 1 Answer

Find angle between two gameobjects? 1 Answer

How I can increase num 1 to num 2 in x time? 1 Answer

Distribute terrain in zones 3 Answers

Calculate surface under a curve from an animationcurve 3 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