I want and abject in my game to scale up and down constantly through code. Im trying to not use animation here. I tried the PingPong Method. BUt it Doesnt work as expected. The object just keeps scaling up nonstop.
I know this is because going back and forth the PingPong increments the scale nonstop.
any alternative?
Mycode:
For stuff like this, I always suggest a tweening engine. Leantween and dotween are both free and I think are the better options. I know leantween has it’s own pingpong option and dotween probably does also. Tweening engines are nicely setup to allow handling all things for you just by passing in the values.
Both are on the asset store. Note that Leantween hasn’t been updated much lately on the asset store. There is a git repo that has a slightly updated version, but keep that in mind, just in case.
Your PingPong returns a value between 0 and 5. Every frame, you’re saying, take CURRENT scale, and then add a value that ranges from 0 to 5. The next frame, you are taking the CURRENT scale again, which is slightly higher than the previous frame, and adding to that.
The problem isn’t with PingPong, it’s with your code.
Take a FIXED scale, something that you cached and then add the PingPong value to its components.
Those tweening engines are awesome. But take the time to experiment and understand how they work… we use them EVERYWHERE in our games, but they are complicated beasts. Do lots of experiments!