Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Distance traveled

  • Posted by Melissa Franch on June 19, 2023 at 1:36 pm

    Good afternoon!

    I’m stuck on an expression.

    I have a slider that is animated like a mileage counter. It starts at 0, has various accelerations and decelerations, and ends at 0.

    I want a text layer to return the value of the distance traveled by reading the data from the slider that acts as an odometer.

    I have tried several expressions but they all give me the same error, when the slider goes forward it works fine, but when it goes backwards the distance traveled also goes backwards, ending up at 0 again when the slider ends its animation at 0.

    This is the expression that I have right now and it does what I have commented on.

    Good afternoon!

    I’m stuck on an expression.

    var totalDistance = 0;

    var lastRead = 0;

    sliderCuentaKilometers = thisComp.layer(“Controls Journey”).effect(“Speed”)(“Slider”);

    if (sliderCuentaKilometers >= lastRead) {

    totalDistance += sliderCuentaKilometers – lastRead;

    }

    lastRead = sliderCuentaKilometers;

    distanceTotal.toFixed(2);

    I do not know how to do it…

    Filip Vandueren replied 3 years, 2 months ago 2 Members · 7 Replies
  • 7 Replies
  • Filip Vandueren

    June 19, 2023 at 2:10 pm

    Hello Melissa,

    if I understand correctly, the speed-slider is an actual “km per hour”. You would need to integrate that value to get a running total of the total distance travelled. But because expressions are recalculated every frame and they have no memory of what value they’ve had before, we need to explicitly loop through all previous valueAtTimes() like this:

    var totalDistance = 0;
    sliderCuentaKilometers = thisComp.layer("Controls Journey").effect("Speed")("Slider");
    const timeFactor = 1;
    for (t=0; t<time; t+=thisComp.frameDuration) {
    totalDistance+= sliderCuentaKilometers.valueAtTime(t)*timeFactor * (thisComp.frameDuration/3600);
    }
    totalDistance.toFixed(2);

    I’ve added a timeFactor, because I assume this may not be happening in realtime (otherwise, you would have to keep a steady speed of 120km/h to gain 1 km in 30 seconds).

    You may want to make that a higher value if you want to speed things up.

  • Melissa Franch

    June 20, 2023 at 1:36 pm

    Hello!

    Everything happens in real time, it is a driving simulation that lasts approximately a minute and a half.

    I have tried the expression and it gives me an error.

    It tells me that the timefactor is redeclared.

  • Filip Vandueren

    June 20, 2023 at 10:23 pm

    Maybe your after effects is set up with legacy extendscript as the JavaScript engine ?

    Remove the “const” keyword and it should be fine

  • Melissa Franch

    June 21, 2023 at 11:10 am

    Hello! The problem was the javascript legacy, you were right!

    But.

    Although it does seem to work, at first it goes to 0.01, returns to 0.00, goes up to 0.03, returns to 0 and then it always goes up.

  • Filip Vandueren

    June 21, 2023 at 11:28 am

    It should never go down, unless there are negative values in your slider ?

  • Melissa Franch

    June 21, 2023 at 12:02 pm

    there are no negative values ​​in the slider. The slider starts at 0, goes to 80, goes down to 50, goes up to 90 etc… until it goes back down to 0.

  • Filip Vandueren

    June 22, 2023 at 1:10 pm

    Could you share your project ? If I replicate what you describe I get the correct results.

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