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 swag_swag · Mar 11, 2014 at 11:30 PM · javascriptcolorloopbackground

{Solved}Need help making infinite loop for color changing background using a Vector4.Lerp with Mathf PingPong variable.

Here's my script so far, I just need to make it so it loops infinitely. As of right now, it goes from one color, to another, then back, but stops. I need it to keep going back and forth.

 #pragma strict
 
 function Start () {
 }
 
 function Update () {
 
 renderer.material.color = Color.Lerp(Vector4(0.82,0,1,1), Vector4(1,0.83,0,1), backgroundLoop(0.1 * Time.time));
 }
 
 function backgroundLoop(t: float) : float 
 {
 var v = Mathf.Repeat(t, 2);
 return t < 1 ? t : 2 - t;
 }
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 Bunny83 · Jan 19, 2017 at 06:38 AM 0
Share

robertbu already mentioned $$anonymous$$athf.PingPong which is the easiest solution. However the proplem in your "custom" pingpong implementation is that you don't use your "v" variable but your "t". That's why it "stops" after the first ping pong. You do not use the repeated value. So you want to simply do:

 t = $$anonymous$$athf.Repeat(t, 2);

ins$$anonymous$$d of

 var v = $$anonymous$$athf.Repeat(t, 2);


ps: $$anonymous$$athf.PingPong does basically the same thing, just with a slightly different implementation:

 // C#
 public static float PingPong(float t, float length)
 {
     t = $$anonymous$$athf.Repeat(t, length * 2f);
     return length - $$anonymous$$athf.Abs(t - length);
 }

2 Replies

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

Answer by robertbu · Mar 11, 2014 at 11:38 PM

First off, Why use a Vector4? Just use a Color. Second, you can get it to cycle using either Mathf.PingPong() or Mathf.Sin() depending the feel you want. Sin will be more eased at the ends and often feels more natural:

 #pragma strict
 
 var speed : float = 1.0;
  
 function Update () {
     var t : float = (Mathf.Sin(Time.time * speed) + 1) / 2.0;
     renderer.material.color = Color.Lerp(Color(0.82,0,1,1), Color(1,0.83,0,1), t);
 }

And if you prefer Mahtf.PingPong() the calculation of 't' would be:

 var t : float = Mathf.PingPong(Time.time * speed, 1.0);

Comment
Add comment · Show 1 · 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 Bunny83 · Jan 19, 2017 at 06:45 AM 0
Share

Accepted since OP hasn't been on since end of 2014.

avatar image
2

Answer by KarlKarl2000 · Jan 19, 2017 at 04:51 AM

For those that arrive at this thread years later. A small update from my own use case.

RobertBu's answer works . But due to the age of the answer (and unity build), I had to do some update tweaks

Below is my hack:

  renderer.material.color = Color.Lerp(Color.red, Color.black, t);

Obviously the color.red etc are examples only.

Sharing is Caring https://twitter.com/IndieNendoroid

Comment
Add comment · Show 1 · 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 Bunny83 · Jan 19, 2017 at 06:43 AM 0
Share

What do you mean by

due to the age of the answer (and unity build)

? The code still works the same. Also i don't see what "tweak" you have used in your case. You do exactly the same as robert. $$anonymous$$aybe you haven't realised that the code is written in UnityScript and not in C#?

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

22 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

Related Questions

{Solved}Trying to add color loop to background using Vector4 2 Answers

Help with simple scripting? 1 Answer

Access ColorCorrectionCurves values from javascript? 2 Answers

How to stop a for loop before it completes it's full cycle? 1 Answer

Switching color of light 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