20 Replies Latest reply on Oct 7, 2018 8:20 PM by rosannefan

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

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

• 1. Re: How to create a simple bobbing up and down motion expression?

Try this:

amp = 100;

freq = 1;

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

value + [0,y,0]

Dan

• 2. Re: How to create a simple bobbing up and down motion expression?

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

• 3. Re: How to create a simple bobbing up and down motion expression?

Hi Dan,

Please can you tell me which wiggle preset you use?

Many thanks,

Lisa

• 4. Re: How to create a simple bobbing up and down motion expression?

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

Dan

• 5. Re: How to create a simple bobbing up and down motion expression?

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

• 6. Re: How to create a simple bobbing up and down motion expression?

This should work:

amp = 100;

freq = 1;

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

value + [x,0,0]

Dan

• 7. Re: How to create a simple bobbing up and down motion expression?

Amazing, it worked! thank you Dan!

• 8. Re: How to create a simple bobbing up and down motion expression?

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.

• 9. Re: How to create a simple bobbing up and down motion expression?

These are Position expressions. Is that what you meant?

Dan

• 10. Re: How to create a simple bobbing up and down motion expression?

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

• 11. Re: How to create a simple bobbing up and down motion expression?

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])

• 12. Re: How to create a simple bobbing up and down motion expression?

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])

• 13. Re: How to create a simple bobbing up and down motion expression?

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])

• 14. Re: How to create a simple bobbing up and down motion expression?

How do I do the same thing but for rotation?

• 15. Re: How to create a simple bobbing up and down motion expression?

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 + [R])

• 16. Re: How to create a simple bobbing up and down motion expression?

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?

• 17. Re: How to create a simple bobbing up and down motion expression?

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

• 18. Re: How to create a simple bobbing up and down motion expression?

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

• 19. Re: How to create a simple bobbing up and down motion expression?

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

• 20. Re: How to create a simple bobbing up and down motion expression?

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