How to Automatically Animate a Connecting Line in After Effects

So I apologize that I couldn't think of a great way to describe this effect in the title but bare with me! Say you have two objects in After Effects that you want to animate around your screen and them to be connected by a line but you don't want to have to go in and animate the ends of the lines to match up with the bits that they're connecting. How do you do that? Let's dive right in!

Once I've gotten After Effects open I'll make a new composition that's 5 seconds long, just for the sake of example we don't need to make a super long experiment here.

Next I'll want to make the elements that are going to animate around the screen that the stretchy line will be connected to.

To do that let's make two little solids, with Command Y or by selecting Layer > New > Solid, I'll make a 100 by 100 square. It can be any color you like so I decided to make it a nice mint color because I could really go for some gum right now. I'll name it Object 1 so I can keep track of it and quickly differentiate it from the second object we're about to make. It helps to label things clearly, especially when you're working with a lot of similar layers.

Now I'll select it in my timeline and hit Command D or select Edit > Duplicate from the drop down menu to make the second end of the line that will be animating around. I'll rename it to be Object 2 so again I can keep things straight when I'm working on my project.

Now normally we would want to go in and set up the line before we go to animate if that's what we know we want to do, but this time let's go crazy and do a little animating first before we apply our line effect, that way it will be cooler when the line snaps and you see it respond to your already made animation.

So let's go into Object 1's position by selecting it on the timeline and hitting P to bring up it's position properties. I'll hit the stopwatch icon next to it to turn on the keyframes so we can animate it, and at every second I'll move the position of Object 1. For the final position at 5 seconds I'll copy that first keyframe by selecting it and hitting Command C and pasting it with Command V at 5 seconds, just so it animates in a loop.

Now I'll do the same thing for Object 2, although I'll make sure that for each keyframe it goes in a different direction than it did in Object 1, that way we can see the line really stretch and respond. You don't have to be exact or precise with this you can just move them around randomly, we're just trying to get some animation onto the screen so that we can see the line respond to it once we've added our desired effect.

Next, we'll want to make another new solid. This is where our line effect is going to be added to, so since we're not actually going to be seeing the solid because the line is going to over write it, for these kinds of layers I like to use a neon pink or green so it stands out in my timeline. So by hitting Command Y I'll make a new solid, but unlike before I'll want to make it my full composition size of 1920 by 1080. I'll name it something clear and helpful like Line, even though that's not super exciting it'll make it easy to see what layer is what.

In my Effects & Presets window I'll type in Beam and click and drag it onto my Line solid. Or in the drop down menu for Effect I'll choose Generate > Beam with my Line solid already selected.

Once the effect is applied to your layer you'll notice that the layer itself has disappeared and has been replaced with a kind of crumby looking version of a Star Wars blaster beam.

 This is going to become our line that connects our two objects and will automatically animate and connect them.

So first we'll want to change our beam color so it's not the boring default. In the Effect Control window you'll see the Inside Color and Outside Color options, I want my beam to be a solid color and not have any gradient so I'll simply make these two colors the same. I'm gonna click the little eye dropper icon next to them and select the color of my solids, so everything is the same color. Of course you can make yours whatever color you'd like though.

Now I want to make it have a sharp edge, like my solid objects that I've animated, rather than having it have the fuzzy edge of the laser beam. To do that I'll select the Softness percent in the Effect Controls window and make it 0 percent.

That gives me a nice crisp edge for my line.

Now you'll notice that to either side of our line, formerly laser beam, there are two dots. These are the ends of our line. Normally the beam effect is intended to be done just like a Star Wars blaster where you're shooting a beam across your screen. In this case however we're changing it into a solid line, and we don't want it's length changing for the time being.

So let's stretch out our line to reach from end to end. Again in the Effect Controls window we'll take our Length percent and make it 100&, that way it reaches from our line's starting point to our line's ending point.

Now if we wanted to we could animate this line by moving the start point and end point around our screen, and even try to animate it so it follow around our solids that we've animated previously. However there is a much easier way to do this that will both eliminate a lot of work for us (hurray!) and make it so that our line stays completely stationary in where it's attached to our object and not give us any kind of weird "floaty" feel to it connecting to our object. We'll do this by parenting.

Much like when we were making an expression in After Effects, we'll want to Option or Alt click the stop watches next to our line's starting point and ending points. This will bring up the expression window, but rather than writing an expression we'll use something called the Pick Whip to assign each of these values to another bit within our project.

We'll click and hold onto the little swirly spiral icon that has appeared beneath our start and end points in our timeline in the new expression control property that has been brought up by Option or Alt click the stop watch. That's the pick whip.

Next we'll want to drag the pick whip to the position of one of our two object's position property. We can't just apply it to our object as a whole because After Effects needs to know which property of the solid it should be paying attention to.

Once you hover over one of the solid's position attribute you can simply release your mouse click and you should see the end of the line snap into position where ever the solid is.

Now simply repeat this step for the other point in your line and onto the other object.

Now you should see that your line is connecting your two objects. If we click one of our solids we can move it around the screen and see that the line with always stay attached to it no matter where we move it.

Pretty snazzy right? This helps because now we only have to worry about animating the objects rather than both the objects and the line. It also makes it easy if we want to go back and change anything down the road. If we change one of the object's animation, we automatically update the line animation and not have to worry about breaking it or anything. It's pretty handy if you want to make this effect!