• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
• Make an expression run every few seconds

# Make an expression run every few seconds

New Here ,
Jul 16, 2021 Jul 16, 2021

Copied

I am trying to create a jittering effect for an object in after effects.  To do this I used an expression to generate random values between specific cordinates for the position.  The problem is this code is executing every single frame so it looks terrible.  To avoid this I tried using if/then statements using time.  However, I keep getting errors stating that time is an undefined value.  If there is a way to fix my code or a simpler way to accomplish the same goal please let me know.

Here is my code:

if(time == 0){
p = [960,540];
}else{
if((time % 2) == 0){
p = random([950,530],[970,560]);
}
}
p;

TOPICS
Error or problem , Expressions , FAQ , Scripting

Views

117

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
3 Replies 3
Community Expert ,
Jul 16, 2021 Jul 16, 2021

Copied

The code wouldn't do what you want, anyway, so it's a moot point, but you have an extraneaous curly bracket that may throw things of. Anyway, if you really want to switch positions every two seconds, all it needs is to control the random seed:

seedRandom(Math.floor(time/2))

X=random(950,970);

Y=random(530,560);

[X,Y]

Mylenium

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Expert ,
Jul 17, 2021 Jul 17, 2021

Copied

If you add this at the beginning of your expression, it will only be executed once per second:

posterizeTime(1);

If you do

posterizeTime(2);

it will be executed twice per second

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Expert ,
Jul 17, 2021 Jul 17, 2021

Copied

LATEST

Halfway between 950 and 970 is 960 and the range is 10.

Halfway between 530 and 560 is 545 and the range is 15.

If the motion is random, it is highly unlikely that you would notice the 2.5-pixel difference between X and Y so just positioning the layer at 960, 545, and adding wiggle(4, 12.5) would give you a jitter that is close to the right range. If you want to make the movements sharper Pre-compose the layer moving all attributes, open the Composition settings of the new comp and change the frame rate to 6fps, click on the advanced tab, and set the comp to preserve frame rate when nested.

You will get something like this:

That's the simplest way I know of.

If you want the precise range for X and Y, separate dimensions. There is no need to monkey with time at all unless you want random sections with no movement.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Resources
Troubleshooting FAQs
After Effects Community Recap
Getting started with After Effects