Skip to main content
Inspiring
November 5, 2023
Question

I want to trigger an animation when two layers overlap using For-Loop

  • November 5, 2023
  • 2 replies
  • 572 views

Hi guys, I'm a novice when it comes to expressions. 

So what I want to do is trigger an animation on the Opacity setting going from let's say 25% to 75% taking .5 seconds, when one layer overlaps another. Right now I've got the overlap working but it snaps from 25% to 75%. And I'm guessing I'd have to use a for loop statement for this. I don't think just a linear() statment will work. I tried looking up for loops online but I mostly get the loopOut statments. Any help would be appreciated. Right now my code looks like this and I'm using sliders to adjust the opacity, duration and layer selection of the overlap. 

 

var lyr = effect("Collision Controls")("Select Collision Layer");

var lyrCol = lyr.transform.position;

var nA = effect("Collision Controls")("Collide ON");

var nB = effect("Collision Controls")("Collide OFF");

var dur = effect("Collision Controls")("Duration")

var Alpha = thisLayer.sampleImage(lyrCol, [1,1]) [3];

 

if (Alpha < 1){ nB }else{ nA }

 

Thanks!

 

This topic has been closed for replies.

2 replies

Dan Ebberts
Community Expert
Community Expert
November 5, 2023

It's actually kind of tricky. Expressions have no memory, so when your expression wakes up at each frame, it won't know whether or not your opacity ramp has been triggered. If your expression determines that the overlap currently exists, it will need to go back in time, frame-by-frame, to find out when the overlap first occurred (or until it has gone back 5 seconds) to determine where it should be in the opacity ramp. sampleImage() has a built-in time parameter you can use for this. It can get pretty complicated, especially if it's possible for the overlap to end (and what should happen in that case). In any case, there's a bit of code involved and it will include a loop of some kind, but the details will depend on exactly what you want to happen in all situations.

Inspiring
November 5, 2023

Thanks. I think I understand. I did find this  https://www.youtube.com/watch?v=eEEd-YuBHn0

It's not exactly what I was looking for but, you know close enough right now. It uses distance variables between objects to change the values instead of the objectes litterally being on top of each other.

 

 

Mylenium
Legend
November 5, 2023

No need to reinvent the wheel:

 

https://www.motionscript.com/design-guide/collision.html

 

Mylenium