Longtime user of Creative Cow, but this is my first ever post.
I've recently been learning After Effects expressions. I have a subcomp moving across the screen from left to right and I'm trying to get it to reverse direction when it reaches the halfway point of the screen. I have the following expression on its position property and I can't understand how it doesn't work:
direction = 1;
speedMultiplier = 500;
if (transform.position > thisComp.width / 2)
direction = -1;
[time * speedMultiplier * direction, thisComp.height / 2];
Using the same if statement I can change its color using a fill mask, so why can't I dynamically effect the transform property in a similar way?
I'm used to programming in Processing, Monogame, Unity, etc, and this sort of thing works fine in those environments - flipping a variable to a negative number and then using it to change direction. Is there some limitation in the way scripting works in AE that means this just won't work?
Any help greatfully appreciated!
Thanks in advance.
It's not simple. Expressions have no memory, so something like this in a position expression:
will retrieve the pre-expression value, as if the expression never existed. So there is no way for an expression to know anything about what it did on previous frames, except be re-creating the past. For what you're trying to do, you need the expression to
1) determine if the x value is greater than half the comp width
2) if so, locate the frame where the x value goes from less than half the comp width to greater than half the comp width
3) calculate how long ago that was
4) multiply that time by 500, and subtract the result from half the comp width to get the current x postion
Thanks Dan. That certainly isn't simple.
I can see now why some of the sample expressions on your site are more complicated than I expected them to be at first. I'll take a look at your collision detection one for some ideas on how to achieve what I want here.
Nothing like a challenge...!