How to create a simple bobbing up and down motion expression?
Copy link to clipboard
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
Copy link to clipboard
Copied
Try this:
amp = 100;
freq = 1;
y = amp*Math.sin(time*freq*Math.PI*2);
value + [0,y,0]
Dan
Copy link to clipboard
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
Copy link to clipboard
Copied
Hi Dan,
Please can you tell me which wiggle preset you use?
Many thanks,
Lisa
Copy link to clipboard
Copied
I'm not sure what you mean. I don't use a wiggle preset.
Dan
Copy link to clipboard
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
Copy link to clipboard
Copied
This should work:
amp = 100;
freq = 1;
x = amp*Math.sin(time*freq*Math.PI*2);
value + [x,0,0]
Dan
Copy link to clipboard
Copied
Amazing, it worked! thank you Dan!
Copy link to clipboard
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
Copy link to clipboard
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
Copy link to clipboard
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.
Copy link to clipboard
Copied
These are Position expressions. Is that what you meant?
Dan
Copy link to clipboard
Copied
Yes that was! Thank you so much! How can I get this to start at a specific time?
Copy link to clipboard
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])
Copy link to clipboard
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])
Copy link to clipboard
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])
Copy link to clipboard
Copied
How do I do the same thing but for rotation?
Copy link to clipboard
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 +
Copy link to clipboard
Copied
Dan you mad man that is incredible
Copy link to clipboard
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?
Copy link to clipboard
Copied
You could start the expression with this to set the frame rate:
posterizeTime(15)
Dan
Copy link to clipboard
Copied
great, simple and effective. Thanks!
Copy link to clipboard
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!
Copy link to clipboard
Copied
Just change the value of the amp variable to something smaller.
Dan
Copy link to clipboard
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


-
- 1
- 2