Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions change direction with expression

  • change direction with expression

    Posted by Peter Smith on February 23, 2018 at 2:53 pm

    Hi everyone,
    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[0] > 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.

    Peter Smith replied 8 years, 2 months ago 2 Members · 2 Replies
  • 2 Replies
  • Dan Ebberts

    February 23, 2018 at 5:15 pm

    It’s not simple. Expressions have no memory, so something like this in a position expression:

    transform.position[0]

    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

    Dan

  • Peter Smith

    February 23, 2018 at 8:35 pm

    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…!

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy