Jhonatan da Silva

Jhonatan da Silva

Modeling a cart pole system in Unity

Series of tutorials

This is a series of posts describing how to create an inverted pendulum (or cart pole system) with Unity and ML Agents.


  1. Finished reading first tutorial in the series.
  2. Knowledge about the very basics of Unity.

What is the idea?

Now that you understand what a cart pole system is. We can start to move on to Unity. One thing to keep in mind while exploring new ideas, it's a good idea to start with a simple example, and then move on to more complicated ones.

So if you are reading this in order to create your on models, or to move into more complicated things, even in your project, start simple, take a step by step approach, and you'll be likely to find yourself with a final project in the end.

With that on mind, we could start with a complex model like that, but this would imply to learn a lot of things to just try to validate your idea.

Cart Pole System

Instead, we'll focus first on simulating the core mechanics of the inverted pendulum.

Cart Pole System

Creating the structure

For this, we only need a box and a cylinder

Cart model (Box)

Cart Pole System Box

Pendulum model (Cylinder)

Cart Pole System Pole


As we need the system to act regarding physics, we need to also add a rigidbody component to each object.

Cart Pole System Rigidbody

You also need to add the rigid body to the pendulum. With the addition of a ridigbody on the cart, you can restrict the movement of the cart for only the X position (as if it was connected to a precise spindle).

Cart Pole System Constraints


When the cart moves, the pendulum needs to move indirectly by the force applied to the cart. In Unity, one way to do this, is to create a joint, connecting the two rigidbodies. In this case, you can create a Hinge Joint

You need to add the Hinge Joint in the same way you added the Rigidbody, but only in the pendulum model. You also need to specify in the join to what object the pendulum is connected, in this case, the Cube, if you've not renamed the model.

Cart Pole System Hinge Joint

Final considerations

With this, we can start the implementation of the movement of the pendulum.