How to Draw a Physics Line with Your Finger in Unity 2D
How to draw a line with your finger in Unity 2D that a physics body bounces off: spaced points, Edge Collider 2D, length limit, lifetime and common traps.

The player drags a finger across the screen, a line appears behind it, and a falling object bounces off that line as if it were a wall. To draw a line with physics in Unity 2D, you need three things: a list of touch points with a minimum spacing, an Edge Collider 2D built from those points, and rules for when the line ends and when it disappears.
We show the approach using our game Dreamica (App Store, Google Play). It is an adventure game about falling: a bubble carrying a sleeping child drops through a world full of obstacles, and the player draws lines for it to slide along or bounce off. The line is the player's only tool, so it has to behave exactly the way it was drawn and must never leave anything invisible behind.

Step 1: A line as a list of points
A line is a sequence of points in the game world. When the finger goes down, create a new line with a single point. While the finger stays down, convert its screen position into world coordinates and add a new point whenever the finger has moved farther than the minimum spacing. When the finger lifts, close the line.
on press:
new line, first point = finger position in world
length = 0
while finger is down:
p = finger position in world
if distance(last point, p) > minimum spacing:
length += distance(last point, p)
add p to line
redraw line and its collider
if length > limit or budget is used up: close line
on release: close line
The minimum spacing matters more than it seems. Touch input is read every frame, so a slow stroke produces dozens of points almost on top of each other. A collider made of very short segments costs more to compute, and finger jitter shows up in it as tiny teeth that make the body hop around unpleasantly. In Dreamica we add a point only after the finger moves a quarter of a world unit. The line width is 1.5% of the screen width, so it looks the same on a phone and a tablet.


Step 2: A collider from the points
Edge Collider 2D is a good fit for a line. Unlike a polygon collider, it does not need to be closed; an open polyline is enough. Set its points again after every new point so the collider grows along with the line.
The line has thickness, though, and the collider should have it too. There are two ways to do this:
- Center line with a radius. The collider points are the same as the line points, and you set the Edge Radius property to half the line width. The collider then becomes a "thick" line with rounded corners. This is the simplest option if you draw the line with a Line Renderer component.
- Line outline. The collider traces the edge of the rendered strip: one side out, the other side back, and it joins the end to the start. In Dreamica we draw lines with the Vectrosity plugin (a library for drawing vector lines), which builds the collider exactly this way. The advantage is that the collider matches what the player sees, even in sharp bends.


The line does not need a Rigidbody 2D: it is static, and only the body that hits it moves. Watch out for one limitation from the documentation: an Edge Collider does not collide with another Edge Collider. So two drawn lines do not collide with each other (which is usually what you want), but if the player also had an Edge Collider, it would fall through the line. Give the player a circle or polygon collider.
Step 3: Length limit and budget
Without a limit, the player can cover the whole screen in lines and the game stops making sense. Dreamica limits the line in two ways:
- The length of a single line is measured as a fraction of the screen width, not in world units, so a wider display does not make the game easier. The base limit is 35% of the screen width, and each of ten in-game upgrades adds 9.5%, up to 130%.
- Mana is a shared budget for all lines. Each added point costs 10 mana, and mana refills over time. When it drops below the cost of one point, the line closes.
Track the length as you go, with every added point, rather than calculating it afterwards from the finished line. A quick tap without movement leaves the line with a single point, and the length calculation built into a line-drawing library can fail on such a line. A running sum of segments is cheap and always valid.
Step 4: Lifetime and fading
A drawn line in Dreamica lives for 2 seconds and then disappears along with its collider. The short lifetime forces the player to keep drawing and also protects performance: there are never many colliders in the scene at once.
To let the player see which part is fresh, each segment starts white and "cools" into the color of the current world over 0.3 seconds. This is done with vertex colors: for each point, store the time it was added, and every frame recalculate the segment color based on its age. At the same time, the whole line gradually becomes transparent according to how much of its 2 seconds is left.
every frame, for each line:
alpha = 1 - line age / lifetime
for each segment s:
k = clamp((now - creation time of segment s) / cooling time, 0, 1)
segment color = blend(white, world color, k), with alpha alpha
if line age >= lifetime: destroy line and collider

The glow on the line comes from Bloom with an HDR material. The article on glow and 2D lights in Unity URP explains how to set it up so that only the line glows and not the entire UI.
Step 5: A fast body and a thin line
The line is thin, and the falling object can be fast. With the default discrete collision detection, Unity checks only the body's position after each physics step. If the body moves farther in one step than the line is thick, it skips over the line and the collision never happens. The documentation for the Discrete mode states this directly: fast bodies can overlap a collider or pass through it.


There are two solutions, and you can combine them. Set the body to continuous collision detection (Collision Detection: Continuous), which costs more but never misses a collision. Or cap its maximum speed. Dreamica does the latter: the bubble's speed has an upper limit (4 units per second on the base difficulty), so it moves less than the line's thickness per physics step. The speed cap also makes the game easier to read, since the player has time to react.
Traps that create an invisible obstacle
The worst bug with drawn lines is a collider that stays in the scene without a visible line. The player hits empty space and has no idea why. In practice this happens in three ways.
Pausing mid-stroke
The player is drawing, hits pause, and lifts the finger while the game is stopped. The release happens in a frame when the drawing code is not running, so the line never closes. On the next stroke, the new line overwrites the reference to the old one. The old line, collider still active, stays in the scene and nothing removes it, because it is not in the list of lines running out their lifetime.
The fix: put line closing into a single function and call it on finger release, on pause, and at the start of every new stroke (just in case). Closing means handing the line over to the list that tracks its lifetime and clearing the reference to the line being drawn.
Wrong collider layer
The line has its own layer so you can decide in the collision matrix what it collides with. Set it on the line object right when you create it. If you look it up later, for example by searching the parent object, some lines will slip through and their collider stays on the default layer, where it collides with things it should not.
World shift during a long fall
A game built around falling keeps moving in one direction, and the coordinates keep growing. Precision drops with large numbers, so every so often the world is shifted back toward zero (floating origin). Line points are in world coordinates, so when the shift happens you have to move their points as well and rebuild the collider. Otherwise the line looks right, but the collider stays in the old place.
What this approach does not cover
Drawing is single-finger, one line at a time. Multiple fingers mean a separate in-progress line for each touch. The line is static: it does not fall, bend, or get pushed. If you need a line that behaves like an object (falls, tips over), it needs its own Rigidbody 2D and a closed shape, which is a different task. Particles along the line are out of scope here. If you recycle them through an object pool, set them to simulate even off camera, otherwise they sometimes fail to show up after being taken from the pool.


