Frame Rate Independence?

Is this already frame rate independent because of the Time.time or is there somewhere in the code that I could multiply by Time.deltaTime? I have tried a couple of spots that I thought I could insert, but with very wacky results. I am not well practiced in trigonometry and calc so I am having a hard time wrapping my head around where this would fit in. I will definitely be going through the math course to help me understand it further once I finish this course.

2 Likes

Hi Orion,

What exactly are you trying to do? Maybe your code is already framerate independent.

I don’t know if this is frame rate independent or not, I guess I don’t fully understand if this is happening based purely on time, or if it is operating based on frames because of the update() method.
this is my entire oscillator class.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Oscillator : MonoBehaviour

{

// Start is called before the first frame update

Vector3 startingPosition;

[SerializeField]Vector3 movementVector;

[Range(0,1)]float movementFactor;

[SerializeField] float period = 2f;

void Start()

{

    startingPosition = transform.position;

}

// Update is called once per frame

void Update()

{

    OscillateObject();

}

private void OscillateObject()

{

    if (period == Mathf.Epsilon) {return;} //to stop from comparing to a floating point value since they are inaccuratley compared

    float cycles = Time.time / period;

    const float tau = Mathf.PI * 2; // Constant value of 6.283 same as 2PI

    float rawSinWave = Mathf.Sin(cycles * tau); // going from -1 to 1

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

    Vector3 offset = movementVector * movementFactor;

    transform.position = startingPosition + offset;

}

}

Your code is indeed framerate independent because it uses Time.time. At a specific point in time, your game object will be at a specific position.

4 Likes

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

Privacy & Terms