Hi everyone!
I use the same code as the tutorial for oscillation of one obstacle, but it just moves once and seems that stop working or the values of offset are too small so doesn’t show it works.
Any ideas?
https://community.gamedev.tv/uploads/short-url/dX9Vs48OTfODQetGyLmP1A7f9BT.mp4
Can you copy/paste the code you’re using here?
Add three back ticks ``` before the code so it looks like this.
void SomeCode() {
return;
}
[SerializeField] Vector3 movementVector = new Vector3(10f, 10f, 10f);
[SerializeField] float period = 2f;
// [Range(0,1)] [SerializeField]
float movementFactor; // 0 for not moved, 1 for fully moved.
Vector3 startingPos;
// Use this for initialization
void Start ()
{
startingPos = transform.position;
}
// Update is called once per frame
void Update ()
{
if (period == 0) { return; }
float cycle = Time.deltaTime / period;
const float tau = Mathf.PI * 2;
float rawSinWave = Mathf.Sin(cycle * tau);
movementFactor = rawSinWave / 2f +.5f;
Vector3 offset = movementVector * movementFactor;
Debug.Log(offset);
transform.position = startingPos + offset;
}
1 Like
Thanks for replying. This is the code I am using.
1 Like
Easy fix!
You’re using:
float cycle = Time.deltaTime / period;
Change it to:
float cycle = Time.time / period;
Reason being, that, Time.deltaTime only gives you the time since the last frame, so it’ll always be more or less the same, hence, your obstacle will always end up calculating to be in pretty much the same spot. Time.time is a continuous count that keeps increasing from the time the game was started.
Oh, Thanks a lot.
Yes, it’s solved with your hint.
I appreciate your help
1 Like