Oscillation not working for Y Axis

So when I was using oscillation for one of my levels I tried making it oscillate on the y axis but it just stays still doing nothing. I have double-checked my code with Rick’s and yet it doesn’t work. I’m not sure if this is on purpose or not and if it’s gotta do something with my code here you go:

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

public class Oscillator : MonoBehaviour
{
    Vector3 startingPos;
    [SerializeField] Vector3 movementVector;
    float movementFactor;
    [SerializeField] float period = 2f;

    // Start is called before the first frame update
    void Start()
    {
        startingPos = transform.position;
        
    }

    // Update is called once per frame
    void Update()
    {
        if (period <= Mathf.Epsilon) 
        {
            return;
        }
        float cycles = Time.time / period; // continually growing over time
        
        const float tau = Mathf.PI * 2; // constant value of 6.283
        float rawSinWave = Mathf.Sin(cycles * tau); // going from -1 to +1

        movementFactor = (rawSinWave + 1f) / 2f; // recalculated to go from 0 to 1 so its cleaner
        
        Vector3 offset = movementVector * movementFactor;
        transform.position = startingPos + offset;
    }
}

Thanks!

Hi Saahil,

Welcome to our community! :slight_smile:

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? Try to figure out if the code below the if-statement in the Update method gets executed.

Hi Nina, thanks for the help but the entire time it was working. It was just a subtle movement that I did not notice. Sorry. Thanks!

Fantastic! Interpreting data is a crucial part when developing solutions. Never rely on what you see. Always check the values during runtime in your console (or script editor). :slight_smile:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms