Moving obstacle teleports when game starts

My oscillating obstacle teleports to 0,x,0 when the game starts (x being the position set by the oscillating code) regardless of the starting position i have set it.

Here is the code that i have written so far, Honestly i can’t see where is stuffs up

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

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

    // Update is called once per frame
    void Update()
    {
        if (period <= Mathf.Epsilon) {return;}
        float cycles = Time.time / period; // continualy grows over time

        const float tau = Mathf.PI * 2; // cosntant value of 6.283
        float rawSineWave = Mathf.Sin(cycles * tau); // going from -1 to 1

        movementFactor = (rawSineWave + 1f) / 2f; // recalculated to go from 0 - 1 so its cleaner

        Vector3 offest = movementVector * movementFactor;
        transform.position = startingPosition = offest;
    }
}
1 Like

Hi! You have an equal sign where a + should be.

//Right..............................Here!
transform.position = startingPosition = offest;

Hope this helps!

1 Like

rookie mistake, thanks for the help :smile_cat:

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

Privacy & Terms