X Course Advisor FREE Course Advisor
Watch Tutorials
Using Distance From Camera To Animate in After Effects

Check out our After Effects: More Cartoon Animation Basics course!

In this quick tutorial I’ll show a useful expression to control a 3D layers parameters using its distance to a camera. This has many useful applications and is worth experimenting with.

Step 1 - Set Up Your Scene

Create a new Comp 1280 x 720px and call it ‘Main Comp’.

pic 1

Create a new Solid 600 x 600 px and make it 3D and a new Camera (35mm will do).

pic 2

Create a new Null Object and call it ‘Control’ and make it 3D. With the layer selected create a new Expression Slider. Effects > Expression Control > Slider Control. Name the slider ‘Distance’. Now lock the Effects Control panel so it’s always in view.

pic 3

Step 2 - Create And Offset Some Layers

Duplicate your solid and add this expression to the Position parameter of the new layer by Option (Alt) clicking the stopwatch. thisComp.layer(index-1).position + [0,0,0];

Select the last 0 in the array (for the X,Y,Z coordinates) like so...

pic 4

Now drag the Pick Whip (swirly thing) to the ‘Distance’ slider control. This creates a reference to the ‘Distance’ value for the Z position.

pic 5

The expression essentially gets the position of the layer above (index-1) and adds the ‘Distance’ amount to the Z coordinate of the layer. If you rotate the camera and push up the ‘Distance’ value you should see the new Solid move backwards.

pic 6

If you now duplicate that layer a couple of times you should see all the layers offset in Z space by the ‘Distance’ amount. Cool!

pic 7

Step 3 - Parent the Layers To The Control Layer

Now select all your solids and Pick Whip them to the ‘Control’ layer. 

pic 8

This allows you to move them using only the Position parameter of the ‘Control’ layer.

pic 9

Step 4 - Create The Distance Fade Expression

Start by creating two Slider Controls on the ‘Control’ layer. Call one FadeStart (this will be the distance from the camera that the layer starts to fade) and one FadeEnd (the distance the distance from the camera that layer is fully transparent). Set 1500 for the start and 500 for the end.

pic 10

Now we’ll add our expression.

start = thisComp.layer("Control").effect("FadeStart")("Slider");

end = thisComp.layer("Control").effect("FadeEnd")("Slider");

CameraPos = thisComp.activeCamera.toWorld([0,0,0]); 

LayerPos = toWorld(anchorPoint); 

Difference = length(CameraPos, LayerPos); 

linear(Difference, start, end,0,100);

pic 11

Here’s how it works!

start = thisComp.layer("Control").effect("FadeStart")("Slider");

end = thisComp.layer("Control").effect("FadeEnd")("Slider");

Pick Whip these variables to the new slider controls.

CameraPos = thisComp.activeCamera.toWorld([0,0,0]);

Gets the world position of the camera. 

LayerPos = toWorld(anchorPoint);

Gets the world position of the layer.

Difference = length(CameraPos, LayerPos);

Calculates the distance between Camera and Layer in world space.

linear(Difference, start, end,0,100);

Using the distance result we can remap our ‘start’ and ‘end’ slider values to Opacity values between 0-100 using the ‘Linear’expression.  


You can see as I move the solid closer to the camera it starts to fade. 

pic 12

Now apply this expression to all the solids by selecting the Opacity parameter and choose Copy Expression Only from the Edit menu.

pic 13

Select the solids and choose Paste.

pic 14

If we animate the layers towards the camera by key-framing the position of the ‘Control’ layer we get this.

pic 15

You can see this even works if we rotate the camera.

Here’s a slightly more complex version that uses 3D layers inside nested compositions (with Collapse Transformations) to create a reflection on some album covers. As everything is controlled by the main comp we have to calculate and transfer the world space of one comp to another. Perhaps the subject of another tutorial!!

pic 16

Learn more about After Effects in these video tutorials:


Check out our After Effects: More Cartoon Animation Basics course!

Toby Pitman

Toby Pitman | Articles by this author

For the past 20 years Toby has worked as a professional guitarist, programmer and producer. Clients include Sir Paul McCartney, George Michael, Shirley Bassey, Yusuf Islam, Giles Martin as well as the London 2012 Olympic Ceremonies. He has also worked extensively in TV, Advertising and Film. As well as composing himself he has also worked alongside many composers like David Arnold, Clint Mansell and Simon Franglen on many major film releases. An expert in synthesis and sound design Toby has also lectured for Apple on their Logic Pro music software which he has used since its days on the Atari. He has also worked as an educator for the International Guitar Foundation and the Brighton Institute of Modern Music teaching guitar. In his spare time (of which there is very little) he moonlights as a motion graphics artist specialising in Cinema 4D and After Effects.


You must be logged in to post a comment.
Create an Account  Login Now

What is macProVideo.com?

macProVideo.com is an online education community featuring Tutorial-Videos & Training for popular Audio & Video Applications including Adobe CS, Logic Studio, Final Cut Studio, and more.
© 2018 macProVideo.com
a division of NonLinear Educating Inc.