- Home /
Help with Mathf.PingPong
2 questions. 1) Why doesn't deltaTime work correctly and 2) what do I need to do to get my object to move from where it currently is in the world. With this code it jumps to zero and moves to 5, then returns to zero, back to 5, etc.
transform.position.z = Mathf.PingPong(Time.time * 2, 5);
Answer by _-_ · Jan 02, 2014 at 11:58 PM
It's not exactly clear what you're trying to do, could you be a bit more specific (Like, how/where exactly do you want your object to move in relation to time)? Also, I'm not getting where you use deltaTime at all.
I'm assuming that your object is now moving between 0 and 5 and you want it to move between the object position and 5 or something?
I just want it to move back and forth between 2 spots. It's doing that now but I have my object at 0,0,60 and this sends it all the way to 0,0,0 and makes it move between 0 and 5 on the z axis. I want to know what I can do to make it stay at 60 and move around that location.
Answer by HappyMoo · Jan 02, 2014 at 11:58 PM
deltaTime doesn't work, because functions don't remember prior calls... you just insert something in and get the same output back every time you insert the same input.
so lets say your frametime is stable and you insert 0.0166 into this function, why do you expect a different output every time?
ping pong works like this:
input, output:
0, 0
1, 1
2, 2
3, 3
4, 4
5, 5
6, 4
7, 3
8, 2
9, 1
10, 0
11, 1
12, 2
13, 3
lets say you want to move from where you are now to pos = (10,5,2)
Vector3 target = new Vector(10,5,2);
float speed = 0.03f; // 0 - 1
void Update()
{
transform.position = Vector3.Lerp(transform.position, target, speed);
}
this means it it move 3%(speed=0.03) of the remaining path in every frame, which means it moves faster first and gets slower, because 3% of a path that is getting shorter and shorter is also a shorter distance every time.
Your answer
Follow this Question
Related Questions
Does Mathf.PingPong always have to start at (0,0,0)? 1 Answer
Vector3.lerp and Mathf.pingpong not moving smoothly 3 Answers
MathF clamp not working 1 Answer
Ping pong position using lerp 3 Answers
Mathf function on GetKey 1 Answer