Last week, we detailed how we wanted to implement a conveyer belt functionality within Food Fight, but we needed to determine the best approach that would benefit us without causing more programming issues. Here’s what we came up with!
The Best Solution Is Often The Simplest
We just needed to ‘trick’ the physics engine inside of Unity to do the conveyor actions for us. The physics engine is already running on every item with a rigidbody each frame anyway, so there is little to no performance cost either. To do this, we made a simple cube, sized it to the conveyor belt, and added a box collider as well as a rigidbody (set to kinematic with gravity off). We then removed the mesh renderer component, so the only thing the ‘physics’ object does is deal with the game physics.
And here’s all the code we need to put in our FixedUpdate() Method:
if (isOn) { Vector3 position = rb.position; rb.position -= direction.normalized * settings.conveyorSpeed; rb.MovePosition(position); }
The Breakdown: What’s Happening
Unity’s Rigidbody component holds data for mass, velocity, rotation, and much more. It’s the main point where most programmers are going to deal with physics in Unity.
The best practice working with physics is to use FixedUpdate() which happens at regular intervals (by default every 0.02 seconds) regardless of processor load. This also will allow the conveyor to pause correctly since FixedUpdate is not called when Time.timeScale is zero, which is how we’re pausing our game and the rest of the physics.
We have a direction vector set inside the inspector. This should be a magnitude of 1 anyway, but we wanted to normalize it to make sure. We scaled that by the chosen speed from our player settings, which is a scriptable object we reference from all over the project. In our case, we ended up with a ‘speed’ setting of “only 0.02.” This doesn’t sound very fast at all, at first, unless you consider that it’s happening every frame.
The magic happens in the difference between setting a rigidbody’s position directly and using the MovePosition() method. When simply setting it with the ‘=’ operator, the rigibody is simply given the new position and will update to the new position at the start of the next frame. Generally, this is not a good idea because it does not calculate velocity or anything else that’s useful for doing physics when teleporting that object into the space.

But, we use MovePostion — the “proper” way to move objects around with correctly calculated physics. And we do that to only move the rigidbody back to its original location.
The end effect is that the conveyor’s physics component never actually goes anywhere since the actual transform.postion never gets updated. But each frame has velocity data that is generated from the MovePosition method, and so in interacting with other objects it will get treated as if it’s moving very quickly underneath them. These objects then get ‘dragged’ along by the physics engines calculations for friction.
Takeaways and Conclusions
This approach has its limitations. The biggest is that it cannot be used on a curved path as-is except by splitting the physics elements into separate sections which would act separately.
There’s also no animation of the surface on the conveyor. This might be solved by animating the UV textures, but since we have it split up into different sections, syncing those up would be complicated.
Maybe the perfect way to do this would be with verlets, which can simulate cloth and other flexible materials, and with a customized shader that could animate the surface properly. But for the scope of what we needed, it’s important to recognize what is good enough to move on to the rest of the project.
In our play-testing so far with the conveyor, incorporating this makes two changes that offer more variety to game-play, especially for new players to VR. It provides a source of nutritious food and an easier path to power-ups. The functionality also allows the player to quickly practice with how the game physics will respond to the Oculus Touch controllers.
The other take away from this development is that an endless conveyor system of food is a pretty fun thing to play around with in VR. If you have an Oculus Rift, give our game a try and see what you think!