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);
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?
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.