minDist = 1e6;
closestRot = 0;
for (i = 1; i <= thisComp.numLayers; i++)
{
if (i != index)
{
d = length(position, thisComp.layer(i).position);
if (d < minDist)
{
minDist = d;
closestRot = thisComp.layer(i).rotation;
}}}
closestRot;
Finds the closest layer in the comp and copies its rotation in real time. The closest object controls how this layer rotates.
Contributed by:
Useful for swarm, flock, or system animations where objects orient toward nearby elements - also great for connecting elements or auto-aligned labels.