Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by jeremy91 · Oct 04, 2013 at 07:56 PM · timevaluemathfendless loop

Make value change to anoter over time and back loop

Hello. How could I make a value to go from -1 to 1, then from 1 to -1 and back again. It should be a infinite loop. And every step (from -1 to 1 or from 1 to -1) should be happening over a given time? Thank you!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by robertbu · Oct 04, 2013 at 08:00 PM

You can use Mathf.PingPong() or Mathf.Sin() in Update():

 var val = Mathf.PingPong(Time.time * speed, 2.0) - 1.0;

or:

 var val = Mathf.Sin(time.time * speed);

Mathf.Sin() will give a sinusoidal change between -1.0 and 1.0;

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 robertbu · Oct 04, 2013 at 08:03 PM 1
Share

Seeing @HanClinto's answer, I don't know if you want just -1 and 1, or all fractional values in between.

avatar image HanClinto · Oct 04, 2013 at 08:23 PM 0
Share

I guess I assumed he wanted integers, but after seeing your answer, maybe he wanted the fractional values? :)

Nice clean answers, btw.

avatar image
0

Answer by HanClinto · Oct 04, 2013 at 08:01 PM

The way I do this is the following (taken from a Pac Man game I'm working on where the sprites cycle in a back-and-forth munching loop):

     // Set animation frame
     const int NUM_FRAMES = 4; // In our Pac Man animation, we have 4 frames.  
                               //We want the frames to go 0,1,2,3,2,1 and repeat.
     const float ANIM_SPEED = 0.4f; // Modify this parameter to determine the speed at which the frames progress
     
     int frame = ((int) (this.transform.x * ANIM_SPEED)) % NUM_FRAMES;
     
     if (frame > NUM_FRAMES * 0.5)
         frame = NUM_FRAMES - frame;

     // "frame" will now cycle from 0 to 3 and back again in reverse according to the "x" of the transform.

Edit: So in your case, you have 4 desired values. -1, 0, 1, 0, then repeat. For that, you can do:

     int counter = 0;

     void Update() {
         counter++; // I assume this is going up in some fashion
         int ticktock = (counter % 4); // ticktock is now 0 to 3
         if (ticktock > 2)
             ticktock = 1;        // ticktock is now 0 to 2
         ticktock--;            //ticktock is now -1 to 1
     }

Another way is to use an array to store the values you want to toggle through. This is perhaps one of the easier ways to understand:

     int counter = 0;

     void Update() {
         const int[] values = {-1, 0, 1, -1}
         int ticktock = values[(counter++) % 4];
         // ticktock is now at the desired value
     }
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
avatar image
0

Answer by YoungDeveloper · Oct 04, 2013 at 08:08 PM

Wait stuff is different depending on are you using c# or unity script.

 int a = 1;
 
 for(;;){
 //infinite loop
 // if unity script use yield waitforseconds any function, if c#create new IEnumerator function, copy the for loop the 
 //re because only in that function you can use yield waiforsecobds
 
 //start coroutine from start or whatever
 
 a *= -1; 
 }
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

17 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

Related Questions

Static Variable Doesn't Change ? 0 Answers

Time elapsed between variable change? 1 Answer

Random.range returning similar values 1 Answer

SmoothDamp keeps changing back 0 Answers

How do I increase a speed variable over time? 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