Calculate Distance Between Layers in After Effects

Calculating the distance between two layers in Adobe After Effects allows you to dynamically drive animations, such as scaling an object or fading its opacity as it approaches another layer. This calculation is achieved using the built-in length() expression function, which computes the Euclidean distance between two spatial vectors in either 2D or 3D space. This guide covers the basic expression setup, how to account for parented layers using world space, and how to apply the resulting distance to other layer properties.

The Basic Distance Expression

To find the straight-line distance between the current layer and a target layer, use the length() function by comparing their position values.

  1. Select the property you want to drive with the distance (or add an Expression Controls > Slider Control to your layer to store the raw distance value).
  2. Alt-click (Windows) or Option-click (macOS) the property's stopwatch to enable expressions.
  3. Enter the following code:
var targetLayer = thisComp.layer("Target Layer");
var p1 = transform.position;
var p2 = targetLayer.transform.position;

var distance = length(p1, p2);
distance;

Replace "Target Layer" with the exact name of your second layer. The variable distance returns a single number representing the distance in pixels.

Accounting for Parented Layers (World Space)

If either layer is parented to a null object or another layer, using transform.position returns coordinates relative to the parent rather than the composition space, causing inaccurate measurements. To get the true absolute position, convert the coordinates to world space using toWorld():

var targetLayer = thisComp.layer("Target Layer");

var p1 = toWorld(transform.anchorPoint);
var p2 = targetLayer.toWorld(targetLayer.transform.anchorPoint);

var distance = length(p1, p2);
distance;

This method evaluates the absolute location of each layer's anchor point in the composition canvas, regardless of parent-child hierarchies or complex transformations.

3D Layer Compatibility

The length() function automatically adapts to both 2D and 3D layers. If both layers are toggled to 3D, passing 3-element coordinate arrays [x, y, z] to length(p1, p2) computes the distance across all three spatial dimensions. If you only want to measure the 2D horizontal/vertical distance between 3D layers, ignore the Z-axis by passing only the first two components:

var targetLayer = thisComp.layer("Target Layer");
var p1 = toWorld(transform.anchorPoint);
var p2 = targetLayer.toWorld(targetLayer.transform.anchorPoint);

var distance = length([p1[0], p1[1]], [p2[0], p2[1]]);
distance;

Using Distance to Drive Properties

Once you have the distance value, you can map it to properties like Opacity, Scale, or Gaussian Blur using the linear() interpolation function:

var targetLayer = thisComp.layer("Target Layer");
var p1 = toWorld(transform.anchorPoint);
var p2 = targetLayer.toWorld(targetLayer.transform.anchorPoint);
var dist = length(p1, p2);

// Map distance: at 0px distance, opacity is 100%; at 500px distance, opacity drops to 0%
var minDistance = 0;
var maxDistance = 500;
linear(dist, minDistance, maxDistance, 100, 0);