Trouble with updates


This script here is a basic rotation and elevator script which is toggleable in the inspector, However there is a problem with the elevator section, the platform continuously rises up in the fixed update but every 5 seconds is called back down to its starting position (because this script is for many objects are also has the toggleable option for rotation or elevator then an empty game object wouldn’t be suited for the starting point). The problem I’m having is that it teleports to its starting location but after a few minutes I notice it starts to slightly increase its height ever so slightly from where its supposed to be repositioning itself to. I’m assuming it could be due to the update void (I originally had it as void update so I changed it to fixed update and late updated to test but still the same). I have no idea what the point behind fixed update even is, lol. But other than that I can’t really see any other reason that would cause it to slightly go off course. Any ideas?

As the comments mentioned, you shouldn’t be using FixedUpdate for this. If you are going to use it, then you should at least be using the correct time coefficient.

Replace Time.deltaTime with Time.fixedDeltaTime.

StartCoroutine has much more of overhead than a normal loop so you won’t see many people using the solution you have come up with.

As others have suggested, use delta time and MoveTowards in the update loop. This will move the elevator relative to the time between frames which ensures a smooth movement from the users point of view.

As you mentioned your worry about cluttering the update function, I’d suggest you try making your elevate logic a separate function, if you’re not used to this style in programming I’m sure you’ll quickly see its benefit. It is essential if you are ever working with others or wish to develop modular code, not to mention it is cleaner and easier to understand.