Extended Oscillator Script ! Im so proud!

I just need to share this, cuse im so proud of myself, i added rotation and scaling to the oscillator script, so you can apply everyting at once if you want to :smiley:

public class Oscillator : MonoBehaviour
{
    [Header("Movement Options")]
    [SerializeField] Vector3 movementVector;
    [SerializeField][Range(0f, 1f)] float movementFactor;
    [SerializeField] float movementSpeed = 2f; //Sin Period, higher numbers is slower

    [Header("Rotation Options")]
    [SerializeField] Vector3 roatationVector;
    [SerializeField][Range(0f, 1f)] float rotationFactor;
    [SerializeField] float rotationSpeed = 2f; //Sin Period, higher numbers is slower

    [Header("Scale Options")]
    [SerializeField] Vector3 scalingVector;
    [SerializeField][Range(0f, 1f)] float scalingFactor;
    [SerializeField] float scalingSpeed = 2f; //Sin Period, higher numbers is slower

    Vector3 startingPosition;
    Quaternion startingRotation;
    Vector3 startingScale;

    const float pi = Mathf.PI;
    const float tau = pi * 2;

    private void Awake()
    {
        startingPosition = transform.position;
        startingRotation = transform.rotation;
        startingScale = transform.localScale;
    }
    private void Update()
    {
        ApplyMovement();
        ApplyRotation();
        ApplyScaling();
    }
    void ApplyMovement()
    {
        float cycles = Time.time / movementSpeed; // Continually growint over time
        float rawSinWave = Mathf.Sin(cycles * tau); // going from -1 to 1

        movementFactor = (rawSinWave + 1) / 2; // recalculated to go from 0 to 1

        Vector3 offset = movementVector * movementFactor;
        transform.position = startingPosition + offset;
    }
    void ApplyRotation()
    {
        float cycles = Time.time / rotationSpeed; // Continually growint over time
        float rawSinWave = Mathf.Sin(cycles * tau); // going from -1 to 1

        rotationFactor = (rawSinWave + 1) / 2; // recalculated to go from 0 to 1

        Vector3 offset = roatationVector * rotationFactor;
        transform.Rotate(offset);
    }
    void ApplyScaling()
    {
        float cycles = Time.time / scalingSpeed; // Continually growint over time
        float rawSinWave = Mathf.Sin(cycles * tau); // going from -1 to 1

        scalingFactor = (rawSinWave + 1) / 2; // recalculated to go from 0 to 1

        Vector3 offset = scalingVector * scalingFactor;
        transform.localScale = startingScale + offset;
    }
}
5 Likes

Hello!

I made some adjustments on the Rotation behavior.
I didn’t liked the way it worked on your version, so I fixed it to my taste.

I’m sharing it, and I hope you’ll find it useful :grinning_face_with_smiling_eyes:

public class ObjectOscillator : MonoBehaviour

{
    [Header("Rotation Options")]
    [SerializeField] Vector3 rotationVector;
    [SerializeField] [Range(-1f, 1f)] float rotationFactor;
    [SerializeField] float rotationPeriod = 2f;

    Quaternion startingRotation;

    const float pi = Mathf.PI;
    const float tau = pi * 2f;

    private void Awake()
    {
        startingRotation = transform.rotation;
    }

    private void Update()
    {
        ApplyRotation();
    }

    void ApplyRotation()
    {
        float cycles = Time.time / rotationPeriod;
        float rawSinWave = Mathf.Sin(cycles * tau);
        rotationFactor = rawSinWave;
        Vector3 offset = rotationVector * rotationFactor;
        transform.rotation = Quaternion.Euler(offset);
    }
}

i finally got the chance to try out your version
i love it, good job! much better than my veresion!

Well done!!!

Privacy & Terms