Different approach with Mathf.PingPong ? Any disadvantage?

Hello everybody,

i´ve found another solution and wanted to ask if Mathf.PingPong has any disadvantage?
It works fine the only difference I guess is that it doesn´t move that smooth and it is only one line of code.

Thank you :wink:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[DisallowMultipleComponent]
public class Oscilator : MonoBehaviour {

[SerializeField] Vector3 movementVector;

[Range(0, 1)][SerializeField]float movementFactor;

Vector3 startingPos;

// Use this for initialization
void Start () {
    startingPos = transform.position;
}

// Update is called once per frame
void Update () {
    Vector3 offset = movementVector * movementFactor;
    transform.position = startingPos + offset;
    movementFactor = Mathf.PingPong(Time.time/6, 1);        // <<<<< PingPong
}

}

You do lose the advantage of the more natural slowing down/speeding up that Sin, but you don’t have to do the extra math…
Of course… you don’t have to do most of the math anyways… you can use

elapsedtime+=Time.deltaTime //Dont' worry about checking for tau and resetting....
movementFactor=Mathf.Sin(elapsedtime)/2+.5f; 

He just showed us all that extra math to show us about tau and the underbelly of how sin works… but… it turns out…

sin(x) = sin(x+tau) = sin(x+[any value * tau])  

Sin continues to cycle as the angle increases, even as it wraps around.

Privacy & Terms