Can you stagger the movement in time of a Mathf.PingPong ?

Can you stagger the movement in time of a Mathf.PingPong ? is the question, I have a section in my game where it spits out a few rows of space invaders and using the code below in FixedUpdate() they happily appear and move, but I’m using that ‘-2.5f’ float in the command but wanted something which jumps the transform a certain amount rather than smoothly interpolating it if you know what I mean… Am i missing something easy here ? please advise

transform.position = new Vector3 (Mathf.PingPong (Time.time, -2.5f), transform.position.y, transform.position.z); // bounce invaders left&right

Like any repeating thing, there are two discrete concepts here:

  • duration of a cycle
  • phase within that cycle

You have all your invaders cycling on a period of 2.5, so their duration is identical.

You are also using the same time base input (Time.time) so their phase is identical.

If you want each one to cycle at a different rate (duration), then use a different period number for each one.

If you want each one to be at a different phase, add a different offset to each one’s time value before passing it to Mathf.PingPong().

1 Like