- Home /
Best way to make conveyer belts in C#?
First of all, I am not asking for you to script me anything, and I hope unity answers is for questions like this as well. I am making a factory game (Factorio, satisfactory, Dysonsphere program), and I've made the conveyer belt building system. Right now I am trying to figure out how to program the conveyerbelts. A conveyer belt is a 1x1 cube right now, which with many of them forms a longer conveyerbelt, existing of 1x1 conveyerbelts. So I need objects to move along these conveyerbelts, and that is where I need some help with ideas on how to do this the most efficient and optimised way. The game is gonna have a lot of conveyerbelts and objects moving on those belts, so I think it would be best to avoid having a script attached to every single on of them. So I somehow gotta make like a "Conveyer handler" which moves every single object on those conveyer belts. I am thinking about using a for loop which iterates through every object on the conveyerbelts, and uses movetowards to move them to the next conveyer belt. I am not sure if this is the way to tackle it, and if anyone has ideas you are welcome to share it! The game is 3D, and made in C#. Thanks!
Answer by andrew-lukasik · Mar 06, 2021 at 06:21 PM
Define "best".
Some options will definitely be more performant than others but also will require you to create something close to a nested custom game engine that is merely interfacing with Unity for IO.
These more demanding options wont be the "best" necessarily - as those are potentially complex time-sinks and black holes which will sabotage your game progress instead of improving it.
So my personal advice would be to not to go for performance first. Go for gameplay first. BUT try to structure your game logic and data in such a way that is conducive toward refactoring for performance later on . Both MonoBehaviour-heavy and gameObject reference-heavy pathways are super rigid and won't easily allow that.
Worst implementation, on the other hand, is much easier to sketch out:
Everything is a gameObject with RigidBody component
1FPS