Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Increase a value every time an event occurs

  • Increase a value every time an event occurs

    Posted by Simon Francois on January 19, 2023 at 2:55 pm

    Hello!

    I have an element called “Target” moving along the Y axis.

    Along that same axis is a series of squares.

    What I would like is, whenever the Target meets the height of one of those squares, the square spins 90° clockwise. When the Target moves away, the square should retain that value, and later, when they meet again, it will rotate 90° more, so the value keeps increasing through the sequence, as many times as their Y values match. Could anyone please help me there?

    Simon Francois replied 3 years, 7 months ago 2 Members · 9 Replies
  • 9 Replies
  • Simon Francois

    January 19, 2023 at 3:12 pm

    I forgot to mention:

    —Target is randomly moving up and down, but with 100 pixels increments, not wiggling;

    —all the squares are also 100 pixels high, and therefore, Target’s motion and the square-made scale match.

  • Dan Ebberts

    January 19, 2023 at 7:00 pm

    Something like this maybe:

    p = thisComp.layer("Target").position;
    animateFrames = 15;
    triggered = p.valueAtTime(0)[1] >= position[1];
    fCur = timeToFrames(time);
    f = 1;
    fSave = 0;
    n = 0;
    while (f <= fCur){
    if (triggered){
    if (p.valueAtTime(framesToTime(f))[1] < position[1]){
    triggered = false;
    }
    }else{
    if (p.valueAtTime(framesToTime(f))[1] >= position[1]){
    n++;
    triggered = true;
    fSave = f;
    }
    }
    f++
    }
    r = 0;
    if (n > 0){
    r = (n-1)*90 + ease(fCur-fSave,0,animateFrames,0,90);
    }
    value + r
  • Simon Francois

    January 20, 2023 at 8:18 am

    Thanks a lot Dan! It’s working like a charm

  • Simon Francois

    January 20, 2023 at 11:00 pm

    Hi Dan, hi all, and tanks again for your help!

    Based on the expression you sent, I tried to go further, and extend it to a 9×9 grid go squares, with the element named “Target” wiggling all over it, so not one axis anymore.

    As you can see with the attachment, it does work somehow, but most of the times the whole row gets triggered, and, weirdly, only a chuck of a horizontal or vertical row…

    What I want to achieve now is, when approaching a square —let’s say when it is touching it (each saquer is 120×120 pixels, so once the target is nearing it by 60 pixels on X and Y), the square must spin 90} and retains that rotation value until they meet again and it can spin further.

    Here is what I wrote (please don’t mind):

    p = thisComp.layer("Target").position;
    animateFrames = 15;
    Hor = p.valueAtTime(0)[0] >= position[0];
    Ver = p.valueAtTime(0)[1] >= position[1];
    fCur = timeToFrames(time);
    f = 1;
    fSave = 0;
    n = 0;
    while (f <= fCur){
    if (Ver){
    if (p.valueAtTime(framesToTime(f))[1] < position[1] && p.valueAtTime(framesToTime(f))[0] < position[0]){
    Hor = false;
    Ver = false;
    }
    }else{
    if (p.valueAtTime(framesToTime(f))[1] >= position[1] && p.valueAtTime(framesToTime(f))[0] >= position[0]){
    n++;
    Hor = true;
    Ver = true;
    fSave = f;
    }
    }
    f++
    }
    r = 0;
    if (n > 0){
    r = (n-1)*90 + ease(fCur-fSave,0,animateFrames,0,90);
    }
    value + r
  • Simon Francois

    January 20, 2023 at 11:01 pm
    p = thisComp.layer("Target").position;
    animateFrames = 15;
    Hor = p.valueAtTime(0)[0] >= position[0];
    Ver = p.valueAtTime(0)[1] >= position[1];
    fCur = timeToFrames(time);
    f = 1;
    fSave = 0;
    n = 0;
    while (f <= fCur){
    if (Ver){
    if (p.valueAtTime(framesToTime(f))[1] < position[1] && p.valueAtTime(framesToTime(f))[0] < position[0]){
    Hor = false;
    Ver = false;
    }
    }else{
    if (p.valueAtTime(framesToTime(f))[1] >= position[1] && p.valueAtTime(framesToTime(f))[0] >= position[0]){
    n++;
    Hor = true;
    Ver = true;
    fSave = f;
    }
    }
    f++
    }
    r = 0;
    if (n > 0){
    r = (n-1)*90 + ease(fCur-fSave,0,animateFrames,0,90);
    }
    value + r
  • Dan Ebberts

    January 21, 2023 at 7:27 am

    I think you’d have to double up on everything, like this maybe:

    p = thisComp.layer("Target").position;
    animateFrames = 15;
    triggeredH = p.valueAtTime(0)[0] >= position[0];
    triggeredV= p.valueAtTime(0)[1] >= position[1];
    fCur = timeToFrames(time);
    f = 1;
    fSaveH = 0;
    fSaveV = 0;
    nH = 0;
    nV = 0;
    while (f <= fCur){
    if (triggeredH){
    if (p.valueAtTime(framesToTime(f))[0] < position[0]){
    triggeredH = false;
    }
    }else{
    if (p.valueAtTime(framesToTime(f))[0] >= position[0]){
    nH++;
    triggeredH = true;
    fSaveH = f;
    }
    }
    if (triggeredV){
    if (p.valueAtTime(framesToTime(f))[1] < position[1]){
    triggeredV = false;
    }
    }else{
    if (p.valueAtTime(framesToTime(f))[1] >= position[1]){
    nV++;
    triggeredV = true;
    fSaveV = f;
    }
    }
    f++
    }
    rV = 0;
    rH = 0;
    if (nH > 0){
    rH = (nH-1)*90 + ease(fCur-fSaveH,0,animateFrames,0,90);
    }
    if (nV > 0){
    rV = (nV-1)*90 + ease(fCur-fSaveV,0,animateFrames,0,90);
    }
    value + rV + rH
  • Simon Francois

    January 21, 2023 at 8:35 am

    Thanks Dan!! Do you know why the whole row gets triggered instead of just one single square, as shown in the video attached?

  • Dan Ebberts

    January 21, 2023 at 5:29 pm

    Probably because I didn’t read your previous question correctly. 🙂

    Try this one:

    p = thisComp.layer("Target").position;
    animateFrames = 15;
    lim = 60;
    d = p.valueAtTime(0) - position;
    triggered = Math.abs(d[0]) <= 60 && Math.abs(d[1]) <= 60;
    fCur = timeToFrames(time);
    f = 1;
    fSave = 0;
    n = 0;
    while (f <= fCur){
    d = p.valueAtTime(framesToTime(f)) - position;
    if (triggered){
    if (Math.abs(d[0]) > 60 || Math.abs(d[1]) > 60){
    triggered = false;
    }
    }else{
    if (Math.abs(d[0]) <= 60 && Math.abs(d[1]) <= 60){
    n++;
    triggered = true;
    fSave = f;
    }
    }
    f++
    }
    r = 0;
    if (n > 0){
    r = (n-1)*90 + ease(fCur-fSave,0,animateFrames,0,90);
    }
    value + r
  • Simon Francois

    January 22, 2023 at 6:59 am

    Splendid!!! Thanks a lot Dan, this works like magic!

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