Highlighted

## How to create a simple bobbing up and down motion expression?

Contributor ,
Mar 18, 2013

Copied

Hi, I have a 3D object that I want to continuously bob up and down but I'm having trouble finding an expression to do this.

I would use wiggle on the Z except wiggle creates random motion and I need consistant up and down oscillations.

It seems simple but I can't figure it out... any ideas? Thanks.

-Pete

TOPICS
Expressions, How to

Views

36.6K

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

## How to create a simple bobbing up and down motion expression?

Contributor ,
Mar 18, 2013

Copied

Hi, I have a 3D object that I want to continuously bob up and down but I'm having trouble finding an expression to do this.

I would use wiggle on the Z except wiggle creates random motion and I need consistant up and down oscillations.

It seems simple but I can't figure it out... any ideas? Thanks.

-Pete

TOPICS
Expressions, How to

Views

36.6K

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
Mar 18, 2013 0
30 Replies 30
Guide ,
Mar 18, 2013

Copied

Try this:

amp = 100;

freq = 1;

y = amp*Math.sin(time*freq*Math.PI*2);

value + [0,y,0]

Dan

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
Mar 18, 2013 9
Contributor ,
Mar 18, 2013

Copied

Wow Dan, that works perfectly... thanks for the help!

It would be cool if wiggle had an option to control the randomness so I can get the same results as your expression.

-Pete

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
Mar 18, 2013 0
New Here ,
Dec 19, 2016

Copied

Hi Dan,

Please can you tell me which wiggle preset you use?

Many thanks,

Lisa

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
Dec 19, 2016 0
Guide ,
Dec 19, 2016

Copied

I'm not sure what you mean. I don't use a wiggle preset.

Dan

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
Dec 19, 2016 1
New Here ,
Jun 02, 2017

Copied

Hi Dan - your solution is a lifesaver. Pretty new still to a Position Express - if I wanted to do a horizontal shake back and forth rather than up and down like your code, what would that look like? I tried subbing in X for Y values but that didn't work. Much appreciated!

Julie

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
Jun 02, 2017 0
Guide ,
Jun 02, 2017

Copied

This should work:

amp = 100;

freq = 1;

x = amp*Math.sin(time*freq*Math.PI*2);

value + [x,0,0]

Dan

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
Jun 02, 2017 2
New Here ,
Jun 02, 2017

Copied

Amazing, it worked! thank you Dan!

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
Jun 02, 2017 0
New Here ,
Oct 07, 2018

Copied

Hi, Dan -

How exactly are you designating x and y as targets for the expression?  I understand x = or y = _____, but I am getting lost when you write things like [0,x,0] and so forth.  I think arrays when I see [ ], but I don't understand what the arguments are inside those brackets.

For instance, say I wanted to oscillate on both the y and z axis'?  What about cos on y and sin on z?

Many thanks!

Billy B

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
Oct 07, 2018 0
New Here ,
Oct 07, 2018

Copied

Oh wait I think I got it...  Inside of the brackets goes the arguments for the x y and z.  So it's ...

value + [x,y,z]

Here's the code you gave modified to oscillate on the y axis by sin and the z axis by cosine.

ampY = 10;

ampZ = 10;

freqY = 1;

freqZ = 1;

y = ampY*Math.sin(time*freqY*Math.PI*2);

z = ampZ*Math.cos(time*freqZ*Math.PI*2);

value + [0,y,z]

This post really helped a lot!!  Seems like the folks who write the how-to's on the Adobe website are engineers and can't think like a doofus (like me LOL).

Cheers!

BB

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
Oct 07, 2018 0
Community Beginner ,
Jul 31, 2017

Copied

Hey Dan I am kind of new to after effects and I understand expressions but I honestly not sure where you post these lines. Can you help out? Would be much appreciated.

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
Jul 31, 2017 0
Guide ,
Jul 31, 2017

Copied

These are Position expressions. Is that what you meant?

Dan

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
Jul 31, 2017 0
Community Beginner ,
Jul 31, 2017

Copied

Yes that was! Thank you so much! How can I get this to start at a specific time?

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
Jul 31, 2017 0
Explorer ,
Aug 01, 2017

Copied

try subbing this for the last line of Dan's expression.

t=time; // Comp Time //

StartTime=2; // time in seconds | change this to when you want it to start //

if (t < StartTime) value else (value + [x,0,0])

so that the end result is:

amp = 100;

freq = 1;

x = amp*Math.sin(time*freq*Math.PI*2);

t=time;                                // Comp Time //

StartTime=2;                      // time in seconds | change this to when you want it to start //

if (t < StartTime) value else (value + [x,0,0])

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
Aug 01, 2017 0
Community Beginner ,
Aug 01, 2017

Copied

Thanks a ton for the help! How do I format the time that I need to enter? I'm still new to expressions.

This is what my expression lines look like but it starts at the beginning still.

amp = 15;

freq = 1;

y = amp*Math.sin(time*freq*Math.PI*2);

t=time;                                // Comp Time //

StartTime=2;                      // 07:45 //

if (t < StartTime) value else (value + [0,y,0])

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
Aug 01, 2017 0
Explorer ,
Aug 02, 2017

Copied

Future reference  anything inside of forward slashes in expressions are just comments. By using a double slash, that means that section of text, etc are not "seen"by the software.

I highly recommend doing the JavaScript course on CodeCademy's website, it's free and very helpful for first time coders. It's not a direct application to AfterEffects, but most of the information like setting up variables and using if, or, else statements hold true.

To adjust the StartTime, change the "2" to whatever time you want in seconds.

Based on your reference above ^ you want it to start at 7 minutes and 45 seconds. This would be 465 seconds, so...

amp = 15;

freq = 1;

y = amp*Math.sin(time*freq*Math.PI*2);

t=time;                                // Comp Time //

StartTime=465;                      // Start Time of Bobbing effect //

if (t < StartTime) value else (value + [0,y,0])

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
Aug 02, 2017 0
New Here ,
Aug 15, 2017

Copied

How do I do the same thing but for rotation?

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
Aug 15, 2017 0
Explorer ,
Aug 15, 2017

Copied

Instead of setting up for an [X, Y] coordinate, you use the same expression but for a single number ouput.

amp = 15;

freq = 1;

R = amp*Math.sin(time*freq*Math.PI*2);

t=time;                                // Comp Time //

StartTime=465;                      // Start Time of Bobbing effect //

if (t < StartTime) value else (value + )

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
Aug 15, 2017 0
New Here ,
Jun 28, 2019

Copied

Dan you mad man that is incredible

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
Jun 28, 2019 0
New Here ,
Jul 10, 2019

Copied

thanks a ton for this script Dan,

I wonder if there would be a way to add a time interpolation to this expression, so that it animates every other frame - on twos or in threes for example which is very useful for animation techniques.

something like

tStep = framesToTime(x);

where x would be how many frames it holds before advancing to the next interpolation? hope it makes sense?

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
Jul 10, 2019 0
Guide ,
Jul 10, 2019

Copied

You could start the expression with this to set the frame rate:

posterizeTime(15)

Dan

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
Jul 10, 2019 2
New Here ,
Jul 10, 2019

Copied

great, simple and effective. Thanks!

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
Jul 10, 2019 0
New Here ,
Jul 13, 2019

Copied

Dan you After effects legend, is there possibly a way to reduce the distance of the bobbing? so it's a little softer. The expression is perfect but it bobs too far, from the bottom of my composition to the top!

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
Jul 13, 2019 0
Guide ,
Jul 13, 2019

Copied

Just change the value of the amp variable to something smaller.

Dan

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
Jul 13, 2019 0
New Here ,
Jul 03, 2020

Copied

Hey Dan,

I'm currently using this expression for a small project I'm working on, it works amazingly, but I need help with an adjoining element. I'm attempting to add a circular shadow underneath the object I have floating using your expression, but I'm wondering if it's possible to have the shadow underneath expanding and contracting in the same tempo and style as the object above it, does this make sense to you at all?

Best,

Myles

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
Jul 03, 2020 0
Guide ,
Jul 03, 2020

Copied

You might try something like this for Size or Scale (make sure the freq variable matches your other exrpession):

amp = 20;
freq = 1;
s = amp*Math.sin(time*freq*Math.PI*2);
value*(1+s/100)

Dan

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
Jul 03, 2020 0
Community Beginner ,
Feb 12, 2018

Copied

Hey guys, sorry to revive this but the expression is really great. I'm trying to use it for the z-axis and so far it worked the only thing I'm stuck with is how to create only positive values for the amplitude. Any idea on how to do this?

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
Feb 12, 2018 0
Explorer ,
Mar 15, 2018

Copied

Sorry for the late response.

One way to achieve this is by adding Math.abs() to your expression which will return the absolute values instead of true values (aka negative numbers get switched to positive).

Z=Math.abs(amp*Math.sin(time*freq*Math.PI*2));

- Eric

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
Mar 15, 2018 1
New Here ,
Aug 31, 2018

Copied

Hi all,

The expression worked great. But i have no idea on if it is possible to decelerate the "bobbing/bopping" motion.

Do i need to adjust the value or frequency?

Thanks guys! x

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
Aug 31, 2018 0
New Here ,
May 15, 2020

Copied

HOW CAN I STOP THIS?

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
May 15, 2020 0
LATEST
Jul 03, 2020

Copied

Stop or start the effect by controlling amp and freq with sliders then animate the sliders.

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
Jul 03, 2020 0