Hi, guys, I did movement obstacle without using Sin function, just did it before the lecture started. So, my question, could we use this technique also, pros are less calculations and faster code execution, cons - is not depending of frame time completion. On the screen it works without significant issues and quite smooth, cheers.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[DisallowMultipleComponent]
public class Oscillator : MonoBehaviour
{
[SerializeField] Vector3 movementVector;
[Range(0, 1)] [SerializeField] float movementFactor;
[SerializeField] float movingStepPerUpdate = 0.02f;
Vector3 startingPosition;
float direction;
// Start is called before the first frame update
void Start()
{
startingPosition = gameObject.transform.position;
direction = movementFactor < 1 ? 1f: -1f;
}
// Update is called once per frame
void Update()
{
movementFactor += movingStepPerUpdate * direction;
if (movementFactor > 1)
{
movementFactor = 1f;
direction = -1f;
} else if (movementFactor < 0)
{
movementFactor = 0.0f;
direction = 1f;
}
gameObject.transform.position = startingPosition + movementVector * movementFactor;
}
}