what exactly does Time.time do in Mathf.PingPong?
I'm sorry for the stupid question but I can't seem to get my head around this one.
Whenever I see PingPong used, I see it with Time.time, but I don't understand how it works exactly.
From the Scripting-Reference:
Mathf.PingPong
static function PingPong (t : float, length : float) : float
PingPongs the value t, so that it is never larger than length and never smaller than 0.
The returned value will move back and forth between 0 and length.
function Update () {
// Set the x position to loop between 0 and 3
transform.position = Vector3(
Mathf.PingPong(Time.time, 3), transform.position.y, transform.position.z);
}
So...
Mathf.PingPong PingPongs value t (=Time.time), so that it is never larger than length and never smaller than 0.
How does it work, that it PingPongs 'value t'??
Time.time is read-only. Time.time is an ever-increasing value dependent on timescale and seconds-since-game-started.
So how can Time.time be 'PingPonged' to be never larger than length and never smaller than 0?
If it's clear that Mathf.PingPong returns a value between 0 and lenght, the 'value t' must be to somehow set the speed, but why do we want this speed to be an ever-increasing float. And why doesn't this ever-increasing float make the speed actually... you know... increase?? O-o
I have a nebulous hunch, that PingPong probably needs a constantly increasing value to work (though I don't get why...)
So could I also use something like this?:
var counter : float = 1.0;
function Update () {
counter += 0.2;
transform.position = Vector3 (Mathf.PingPong(counter, 4), tr.pos.y, tr.pos.z);
}
Can someone explain to me why PingPong works in such a weird way and doesn't look more like
static function PingPong (startvalue:float, endvalue:float, speed:float): float
which would have the convenient side-effect that we don't have to subtract something if we want to ping-pong around 0?
Greetz, Ky.
Answer by Graham-Dunnett · Jul 29, 2011 at 09:26 PM
1) L = 2 * length
2) T = t modulo L
3) x = L - T
4) return x
As t increase from 0 to length return t. If t is in the range length to 2*length return 2*length-t. This will change from length back to zero. The modulo operation essentially repeatedly subtracts 2*length until the result is in the range zero to 2*length.
Just try it with some numerical values.
To answer your question, time is never changed. The ping-ponged value is returned by the function.
thank you for the accurate answer... took me a few times to read, but I think I got it now... :) Greetz, $$anonymous$$y.
As t increase...
You mean T, not t. The way you've written it is confusing because the capital letter matters given your choice of variables. I get incorrect values if I use t ins$$anonymous$$d of T.
This post really should show the conditional statement in the numeric list as well. Something like this:
L = 2 * length
T = Time.time mod L
if 0 <= T < length:
return T
else:
return L - T
I can't figure out why this comment has a 5. on one of the lines. I've tried everything I can think of to remove it to no avail.
Answer by Owen-Reynolds · Jul 29, 2011 at 11:26 PM
Yes, any increasing value works for ping-pong. Time.time
is the easiest, but if you want to start ping-ponging after a certain event, than using Time.time
will snap you to a randomish spot (if you start doing a length 4 ping-pong at time 7, you'll jump to 1 unit away, coming back to the start.) In that case, your counter method is exactly what you want.
The version of ping-pong you're thinking of is a fire-and-forget. It could be written in a coroutine with yield. Yes, that would be more useful, but coroutines confuse a lot of people and they are tricky to stop. That's probably why iTween is so popular.
PingPong is a simple 1-output math function. Not great, but better than nothing. You give it inputs, it gives you an output. If no inputs change, the output doesn't change. Take a look at Mathf.SmoothDamp. It ramps your speed up, moving you, then coasts down to the end. You still have to call it every frame with an increasing value. You also have to create the currentVelocity variable, start it at 0, and pass it in by reference. SmoothDamp changes it, but the only way it "remembers" it for next time is because you pass it back in.
PingPong could have been written as with a start besides 0, as you suggest: PingPong(time, start, stop)
. Think of pingPong like Mathf.Abs
. You could just use an if -- Abs is just a tiny bit nicer.
Thank you for the follow up on Graham's answer. Clearing this up quite a bit more =) Greetz, $$anonymous$$y.
Answer by cgkaransahu · Jun 05, 2020 at 02:08 PM
var position = new vector3 ( 0, 0, Mathf.PingPong( (SpeedValue*Time.time),EndPos ) ); // This Will Run In Z Axis Only Between 0, Endpos;
//But if you want Custom Minimum Value like = 5, -3, etc. //If you want negative Number then minus that number like this = var position = new vector3 ( 0, 0, Mathf.PingPong( (SpeedValue*Time.time), ( EndPos + MinValue ) ) - MinValue);
//If you want positive Number then Plus that number like this = var position = new vector3 ( 0, 0, Mathf.PingPong( (SpeedValue*Time.time), ( EndPos - MinValue ) ) + MinValue);
Your answer
Follow this Question
Related Questions
Pausing a Mathf.PingPong at the ends 0 Answers
Separate Numbers ? 1 Answer
My enemy cube moves through walls 0 Answers
Mathf.SmoothStep sequence Time problem 1 Answer
how to rotate a gameobject between two angles front and back direction? 0 Answers