using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LV20platform : MonoBehaviour
{
void Start()
{
}
void Update()
{
gameObject.transform.position = gameObject.transform.position + new Vector3(0.02f, 0.02f, 0);
}
}
this is literally my code and somehow it manages to be bugged.
when press play object move as intended sometimes, faster sometimes. but once reloaded, it’s always faster., using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LV20platform : MonoBehaviour
{
void Start()
{
}
void Update()
{
gameObject.transform.position = gameObject.transform.position + new Vector3(0.02f, 0.02f, 0);
}
}
this is literally my code and somehow it manages to bug.
the object moves sometimes as intended, sometimes faster when press play but after reloading scene once, object always gets faster.
The problem you have here is that you don’t reference the framerate you have.
You can do that experiment yourself: run the programm once with nothing else - it should do what you expect.
Then run it once more but before you start it run as many other things as possible so that your computer is capped on performance. You will see a drop in movement speed.
You can solve this by using Time.deltaTime. This variable gives you the length of the last frame, so the time between the current Update call and the last Update call. This way no matter the performance you can now scale the speed to be constant over time, independent of the framerate of your game:
void Update()
{
var speed = 0.02f * Time.deltaTime; // will now move *exactly* 0.02f units PER SECOND - you might want to raise the base value to something higher now
gameObject.transform.position = gameObject.transform.position + new Vector3(speed, speed, 0);
}
Use FixedUpdate with FixedDeltaTime instead. That way your game will not vary its speed with speed of device its running on.
If you use physics you have to use fixed update anyway, or you will have problems.