• Global community
• Language:
• Deutsch
• English
• EspaÃ±ol
• FranÃ§ais
• PortuguÃªs
• æ—¥æœ¬èªžã‚³ãƒŸãƒ¥ãƒ‹ãƒ†ã‚£
Dedicated community for Japanese speakers
• í•œêµ­ ì»¤ë®¤ë‹ˆí‹°
Dedicated community for Korean speakers
Exit
• Expression help: Need comp to flip it's scale base...

# Expression help: Need comp to flip it's scale based on left/right movement

Community Beginner ,
Apr 26, 2024 Apr 26, 2024

Copied

I've got a composition of an animated bug. I have a script that gives it random movement left and right with pauses (so it'll crawl randomly left or right, stop, then crawl again at random intervals).

I'm looking for a way to flip the composition to face the direction it is moving and STAY that way while paused.

I'm currently using this hackey way to flip the X scale depending on the direction it's moving, but it always goes back to facing one way when it stops, when it should be facing the way it was facing when it stopped.

``````var movement = transform.position.velocity;
if (movement[0] < 0) {
transform.scale
} else {
[transform.scale[0]*-1,transform.scale[1]]
}``````

I hope that makes sense?

Any help would be awesome, thanks!

TOPICS
Expressions

Views

120

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 , Apr 26, 2024 Apr 26, 2024

Try this:

``````var movement = transform.position.velocity;
if (Math.abs(movement[0]) < .01){
t = time - thisComp.frameDuration;
while (t >= 0){
if (Math.abs(position.velocityAtTime(t)[0]) < .01){
t -= thisComp.frameDuration;
continue;
}
if (position.velocityAtTime(t)[0] < 0){
transform.scale;
}else{
[-scale[0],scale[1]];
}
break;
}
} else if (movement[0] < 0) {
transform.scale;
} else {
[-scale[0],scale[1]];
}``````

4 Replies 4
LEGEND ,
Apr 26, 2024 Apr 26, 2024

Copied

You need to run it in a loop and check every frame.

Mylenium

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 ,
Apr 26, 2024 Apr 26, 2024

Copied

Try this:

``````var movement = transform.position.velocity;
if (Math.abs(movement[0]) < .01){
t = time - thisComp.frameDuration;
while (t >= 0){
if (Math.abs(position.velocityAtTime(t)[0]) < .01){
t -= thisComp.frameDuration;
continue;
}
if (position.velocityAtTime(t)[0] < 0){
transform.scale;
}else{
[-scale[0],scale[1]];
}
break;
}
} else if (movement[0] < 0) {
transform.scale;
} else {
[-scale[0],scale[1]];
}``````

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 Beginner ,
Apr 26, 2024 Apr 26, 2024

Copied

Perfect! That works great, exactly what I needed. Thank you (again!) Dan Ebberts

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 ,
Apr 26, 2024 Apr 26, 2024

Copied

LATEST

That's great. It turns out that expression will break under certain conditions, so this one is probably better:

``````var movement = transform.position.velocity;
val = value;
if (Math.abs(movement[0]) < .01){
t = time - thisComp.frameDuration;
while (t >= 0){
if (Math.abs(position.velocityAtTime(t)[0]) < .01){
t -= thisComp.frameDuration;
continue;
}
if (position.velocityAtTime(t)[0] > 0){
val = [-scale[0],scale[1]];
}
break;
}
} else if (movement[0] > 0) {
val = [-scale[0],scale[1]];
}
val``````