Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Hitbox + generate a random value

  • Hitbox + generate a random value

    Posted by Simon Francois on May 18, 2021 at 3:52 pm

    Hello everyone, I’m trying something I thought would be simple, but isn’t quite working.

    Basically:

    1. —I have a shape layer, and a null object on my scene. The null object’s position wiggles, and at times matches the position of the shape layer. I set up a hitbox to make this encounter as real as it can virtually be;
    2. —so when when the null object hits it, the shape layer’s position is affected. This works!
    3. —but I also have to generate a random value, between 3 and 12. The value has to be different every time the null object re-enters the shape’s hitbox (I mean: it can be the same value twice or thrice in a row of course, but has to vary most of the time). Here is the expression I wrote (“curseur” is “slider” in French):

    var p1 = thisLayer.transform.position;
    var p2 = thisComp.layer("Cible").transform.position;
    var HitIn = thisComp.layer("Controller").effect("hitbox Inside")("Curseur");
    var HitOut = thisComp.layer("Controller").effect("hitbox Outside")("Curseur");
    var d = length(p1, p2);
    if(d<HitOut){
    seedRandom(1, true);
    var r = Math.round(random(3, 12));
    } else if(d>HitOut){
    seedRandom(0, true);
    var r = Math.round(random(3, 12));
    }
    r

    Any clue why this does not work?

    Thank you in advance,

    Simon

    Simon Francois replied 5 years, 3 months ago 2 Members · 2 Replies
  • 2 Replies
  • Filip Vandueren

    May 19, 2021 at 11:50 am

    The expression has no memory of previous hits, when you ask it to create a random value.

    You have to count the number of times the hit has occured up to the current frame (using a loop and valueAtTime()) , and then you can use that total as a seed for the random value.

  • Simon Francois

    May 19, 2021 at 12:22 pm

    Thank you Filip!

    I figured it, and started to create a counter, but yeah… my computer overheated so I went for a simpler solution.

    Thank you anyway, I really appreciate!

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