- Home /
Does Mathf.PingPong always have to start at (0,0,0)?
pretty much what the title asks. I copied the exaple right from the scripting reference but I noticed that u can't choose where it starts and where it ends like Lerp for example. All you can do is choose the time and the length.
Answer by Bunny83 · Jan 04, 2014 at 01:53 AM
Yes, like you can read in the docs it always ping-pongs between 0 and length. However if you want a different range, just add an offset. You can also create a helper like this:
// C#
float PingPong(float aValue, float aMin, float aMax)
{
return Mathf.PingPong(aValue, aMax-aMin) + aMin;
}
// UnityScript
function PingPong(aValue : float, aMin : float, aMax : float) : float
{
return Mathf.PingPong(aValue, aMax-aMin) + aMin;
}
thanks a lot. I'm gonna test it in a bit. It's a little confusing but I'll figure out which part does what when I test it.
ps: If you want a correct "mapping", you would have to subtract a$$anonymous$$in from aValue when feeding it to $$anonymous$$athf.PingPong. However usually pingpong is only used with a constantly incrementing / decrementing value so it usually doesn't matter since it just "shifts" the ping-pong pattern start by a$$anonymous$$in.
That would make more sense when using $$anonymous$$athf.Repeat to clamp a value between a $$anonymous$$ and max value.
I can't get it to work. Here's my code.
var aValue : float = 10.0;
var a$$anonymous$$in : float = 3.0;
var a$$anonymous$$ax : float = 7.0;
function Update ()
{
}
function PingPong(aValue : float, a$$anonymous$$in : float, a$$anonymous$$ax : float) : float
{
return $$anonymous$$athf.PingPong(aValue, a$$anonymous$$ax-a$$anonymous$$in) + a$$anonymous$$in;
}
goth it to work. function PingPong is confusing as hell. But I added an offset like you said and it works like I want it to. I had already tried this but I didn't know how to get it to work. Anyway here's my working code.
function Update ()
{
transform.position.z = $$anonymous$$athf.PingPong(Time.time, 15) + 5;
}
I think you got the purpose of my helper function wrong ;) $$anonymous$$y function just wraps $$anonymous$$athf.PingPong, so ins$$anonymous$$d of $$anonymous$$athf.PingPong you can use my PingPong:
function Update ()
{
// the value would ping pong between 5 and 20
transform.position.z = PingPong(Time.time, 5, 20);
}
function PingPong(aValue : float, a$$anonymous$$in : float, a$$anonymous$$ax : float) : float
{
return $$anonymous$$athf.PingPong(aValue, a$$anonymous$$ax-a$$anonymous$$in) + a$$anonymous$$in;
}
Your answer
Follow this Question
Related Questions
Vector3.lerp and Mathf.pingpong not moving smoothly 3 Answers
mathf.pingpong 0 Answers
Make my mathf to Wait? :) 2 Answers
using Mathf.PingPong 1 Answer
Mathf.PingPong make the leaf jittering when the speed is changed. 1 Answer