Copy link to clipboard
Copied
Hello, I'm trying to find a way to make an object's size change based on its current velocity. My desired effect would be that the object is at its full size (scale: 100,100) when stationary and that the object shrinks when it is traveling, proportionate to the velocity.
I think expressions is the way to go for achieving this effect, but I'm new to writing expressions in After Effects so I can't seem to get one to function properly. Here is the draft expression I have currently, this expression errors:
xScale = 1/n;
yScale = xScale;
[xScale,yScale];
If (position.speed = 0){
n = 1;
}
If (position.speed > 0){
n = 1+position.speed
Could someone give me some Ae expressions tips to help me understand why this gives an error and also help me correct this expression so that it will function? Pretty please, and thank you so much in advance!
Hi Leviah,
you should consider this resource : Expression language in After Effects
Your code is faulty and to complicated and therefore fails.
There are several syntax errors, for example: if you want to address an attribute of an object, you need the whole "path" from the object to that attribute. "position.speed" won't work, because it is "thisLayer.transform.position.speed".
Also, you should learn the difference between "=" and "==".
Instead of writing all those lines, you can just link the spee
...Copy link to clipboard
Copied
update:
I got an expression to work. My main problem was that I need to define all of the variables before listing the bracketed scale values. I also couldn't get an IF statement to work properly so I did a mathematic-workaround to avoid dividing by zero (tips on IF statements would be appreciated). Here is my current expression:
n = position.speed/3000+1
x = 100/n;
y = x;
[x,y]
Here's one glitch I've encountered with the new expression. The scaling during the motion is smooth except when the object comes to the end of a sequence of keyframes at which point the velocity abruptly changes to 0 and the object subsequently jumps from whatever scale it was to 100-scale. I would like to find a way for the object to simply maintain its previous scale while resting. Is there a way to add an IF statement for that?
Maybe something like "if (position.speed=0){n = *previous value*}"
Copy link to clipboard
Copied
About the glitch:
You can't hold values with expressions. They are calculated frame by frame. The only way to hold a value is to recalculate everything from start to the present frame. This - unfortunately - is a terrible slow code which slows down AE the longer your layer or animation is.
You need (not tested):
myframe = timeToFrames(time);
scaleX = thisLayer.transform.scale[0];
scaleY = thisLayer.transform.scale[1];
for (n = 0; n <= myframe; n++){
timeback = thisComp.frameDuration * n;
factor = thisLayer.effect("Slider Control")("Slider").valueAtTime(timeback);
speed = thisLayer.transform.position.speedAtTime(timeback) * factor;
scaleX -= speed;
scaleY -= speed;
};
[scaleX, scaleY];
*Martin
Copy link to clipboard
Copied
Hi Leviah,
you should consider this resource : Expression language in After Effects
Your code is faulty and to complicated and therefore fails.
There are several syntax errors, for example: if you want to address an attribute of an object, you need the whole "path" from the object to that attribute. "position.speed" won't work, because it is "thisLayer.transform.position.speed".
Also, you should learn the difference between "=" and "==".
Instead of writing all those lines, you can just link the speed one by one to the scale, adding a correction factor for fine tuning. This is the easiest way in most cases, yet not the most sophisticated. To manipulate the factor, the best way is to outsource it on a slider.
This is untested code, but from my friday evening logic, it should run. (You need to put a slider control to the layer effects stack and set it to 1 for the beginning!)
factor = thisLayer.effect("Slider Control")("Slider");
speed = thisLayer.transform.position.speed * factor;
speedToScaleX = thisLayer.transform.scale[0] - speed;
speedToScaleY = thisLayer.transform.scale[1] - speed;
[speedToScaleX, speedToScaleY];
I don't know the dimension of "speed". You should check the values and bring them into a usable scale. You can use the slider for this, but feel free to add constant correction values to the calculation, if necessary. Also, this code covers individual scales for x and y. If you are always at [100,100], you can skip some lines.
Let me know, if this works!
*Martin
Copy link to clipboard
Copied
Thank you for the help martinr84659894 . I wasn't able to get the time sensitive equation to work but I did get one implementing the slide bar working great. Also, thank you for the expression language resource. I've noticed that there is a difference between = and ==, but I haven't found a post explaining the difference, would you mind explaining that for me?
Copy link to clipboard
Copied
Hi Leviah,
Glad you make it work, but what's the matter with the second code?
In programming, you use = whenever you want to assign a value:
a = 5;
a = b;
// value of b is ?
When using == you are comparing two values to each other, checking if they are the same:
a = 5;
b = 2;
if (a==b){
alert ("a and b are the same!");
} else {
alert ("I understand the difference between = and ==!")
};
// what message get's alerted?
Next to == there is also <= (less or equal), >= (greater or equal), != (not the same) and finally you also have === with is the same as == but you are not only comparing the values, but also the type (string or integer or boolaen or whatever). You don't need it in most cases, though.
Hope you understand now.
*Martin
Copy link to clipboard
Copied
I think I do. Thank you again, that was very helpful!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now