Conveyor Belt Physics

This is just a quick post about how I’ve ended up implemented conveyor belt physics. I googled it a bit and many of the solutions involved rollers or applying forces to objects in OnCollisionStay() but these didn’t have the look and feel that I expected given how conveyors work, and I thought the roller option might end up being pretty heavy.

My solution is very simple but works great. I have a box collider with a kinematic rigidbody, then in the fixed update loop I move the rigidbody backwards by setting rigidbody.position, then use MovePosition() to move it forwards, putting it back where it started. This combination results in the actual conveyor belt object not moving but all objects colliding with it acting as if it was moving forwards.

Here’s a quick demo
(click the buttons to change the speed, grab the cube and capsules to see how the physics reacts)

And here’s the script

21 Comments

  • Mike Says

    This solution is pretty nice, but be warned that it has it’s issues! At least for me, in Unity 2017.2.x this solution does not work so well with “high conveyor speeds”, or having a high value for the conveyor speed field. Basically, the higher the value, it seems like the more off the negative Z direction you can be OFF of the physical representation of the game object you can be and have it still be handled by the conveyor. At the same time, the objects end artificially short of the conveyor and fall through the game object. This problem is amplified the higher and higher the value for conveyor speed is. Just something to watch out for that’s probably an edge case.

  • JK Says

    I know this is an old post but I stumbled upon this and it works great! Just wanted to say thanks for keeping this alive for others to find.

  • Mike Says

    Is there anyway to make the player be slowed down while walking the opposite way that the conveyor is moving, because it only moves the player when they stand still. For me at least 😀

  • Kavery Says

    I got this to work but it only ever goes one speed? I’ve tried changing the amount of distance changed with Rigid Body Move Position but a tiny distance moved equals the same influence speed as a large distance. It also won’t go uphill because the gravity negates the movement.

  • Gustave Says

    Thank you for this really elegant solution.
    I spent some time figuring out how to convert this to use RigidBody2D and then scrolled down and saw the comment about how to do exactly that – doh!

  • abel Says

    hello I was trying to use your conveyor belt on the newer unity build but it seem it no longer working.

    • Mark Hogan Says

      What version of Unity are you using? I just tested it in 5.3.5 and it still worked, your conveyor belt object needs a collider and rigidbody attached and the rigidbody should be set to “is Kinematic = true” and “use gravity = false”. Also I imagine if its a mesh collider it will have to be set to “convex = true”.

  • Anonymous Says

    Very clever! This is the most elegant solution I’ve ever seen. 😉

  • thomas Says

    Great! Very clever solution far better than classical one that consists in using OnCollsion event to increase other objects position

  • Solarix Says

    Hey, great script, works very well with objects, but my character doesn’t care at all about it, why?

    • Mark Hogan Says

      Hmm,the character controller doesn’t behave like a normal physics object, I think you’d have to handle it separately

  • Nicholas B Says

    Hi!

    How would I convert this code to a Rigidbody2D ?

    I’m getting ambiguous error between Vector2 and Vector3

    Thanks!!!

    • Mark Hogan Says

      Hi Nick,
      For 2D you would replace the “transform.forward” with the direction the conveyor is moving as a vector2, for example a conveyor moving left to right would be new Vector2(1, 0)

  • Tris Says

    Hi,

    I am completely new to Unity but was looking to test the above code. I’m getting all sorts of errors (I’m using Unity 5 personal)

    ‘UnityEngine.Component’does not contain a definition for ‘position’ and no extension method ‘position’ of type ‘UnityEngine.component’ could be found

    Any help would be greatly appreciated

    • Mark Hogan Says

      Hi Tris,

      The code was originally written in Unity 4 which had the Component.rigidbody shortcut, basically you have to get the Rigidbody component. I have updated the code for Unity 5, give it a go

  • Really works amazing … Many Thanks

Leave a comment

Your email address will not be published.