Expression to target axis
Copy link to clipboard
Copied
Hello,
I'm new to working with expression.
Situation is like this. I have a bunch of dots next to each other, that I want to move on the y-axis but with a delay.
Used this so far:
offset = -.5;
//The offset in time, negative half a second.
p = thisComp.layer("Dot Master");
//The parent layer
t = time + offset;
//The current time minus half a second
p.position.valueAtTime(t);
//Value of the parent's position property at the current time minus half a second
Works fine, but it just follows the master dot on the same path. I just want to target the movement on the y-axis. .position[1] gives me an expression error.
Any easy solution for this?
Copy link to clipboard
Copied
You can separate your child dots position dimensions by right-clicking on "Position" and choosing the command. Then you can apply the master's keyframes just to the Y-Position, then apply the expressions to the children's Y-Positions, and your current expression should work.
Alternatively, the reason you're getting an error is because you're not feeding AE what it needs. Position is an array, so it wants an [x,y] value returned to it.
By not providing a specific array you're having your expression "p.position.valueAtTime(t);" actually feed AE this data:
[p.position.valueAtTime(t), p.position.valueAtTime(t)]
which means it's going the position is going to be the same for X as it is for Y.
You can easily fix this by writing this as your final few lines.
var x = value[0];
var y = p.position.valueAtTime(t)[1];
[x,y];
You don't have to declare a variable of x, you could just write "value[0]" in the x position of the array, but this is cleaner. Value will take the existing value, so you're telling AE to not touch your child dots' x=positions, and only alter the y-positions. Also, you don't have to write "var" before each of your variables, but it's good practice for clarity, and is technically the proper way to write code when declaring variables. Also, notice how when I apply syntax highlighting by showing this code how an editor would see it, the "var" text is a different color letting you easily see what your variables are.
Copy link to clipboard
Copied
Awesome, that worked! I didn't know you could split the position. Good to know and thanks a bunch!

