Mathf : A to B, then back to A.

Hey all,

Are there any commands which would allow me to “lerp” between say 0, then to 50, then back to 0 again, all in the one tidy statement?

Or perhaps someone could suggest a viable alternative?

Cheers :smile:

How about Mathf.PingPong?

Unless I’m mistaken, PingPong will always go between 0 and the set value (in the example it’s 3), which unfortunately won’t work with what I need, because the “0” will always be changing.

Let me clarify: I have an orb flying between 2 points, using a basic vector3lerp, I want it to dip down (on the y axis) during flight then back up again as it reaches the destination. I initially imagined slerp would work find but it seems to do the opposite, travelling up on the Y axis before returning back down again.

Use Mathf.Lerp AND Mathf.PingPong (not tested yet).

Can’t seem to get it working :frowning: Thanks anyway

Any other ideas?

Make a temporary var with the target coords. Lerp to the the temp var and then Lerp back.

Bryan

I made a PingPong function, but after adding in i it should work for you. The duration right now is per element so the whole animation will be duration * 2.

	IEnumerator ScaleBetween(Vector2 min, Vector2 max, float duration)
	{
		float startTime = Time.time;
		Transform trans = transform;
		float elapsed = 0.0f;
		Vector2 from = min;
		Vector2 to = max;
		int i = 0;
		while(i < 2)
		{
			while(elapsed < duration)
			{
				elapsed = Time.time - startTime;
				Vector2 result = Vector2.Lerp(from, to, elapsed / duration);
				trans.localScale = (Vector3)result + Vector3.forward;
				yield return 0;
			}
			startTime = Time.time;
			elapsed = 0;
			
			Vector2 swap = from;
			from = to;
			to = swap;
			i++;
		}
	}