All Articles After Effects
Using Distance From Camera To Animate in After Effects
Toby Pitman on Sat, April 26th | 0 comments
Take control of your 3D layers in After Effects with this useful expression that allows you to animate using Distance from Camera. Toby Pitman shows how inside, with video examples, too.

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.  

fade


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:

https://www.askvideo.com/courses/application/aftereffects

Comments (0)

You must be logged in to comment. Login Now

Feedback
Course Advisor
Don't Know Where To Start?
Ask A Course Advisor
Ask Us!
Copy the link below and paste it into an email, forum, or Facebook to share this with your friends.
Make money when you share our links
Become a macProVideo.com Affiliate!
The current affiliate rate is: 50%
Classes Start Next Week!
Live 8-week Online Certification Classes for: