Skip to main content
Participant
April 26, 2024
Answered

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

  • April 26, 2024
  • 2 replies
  • 486 views

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!

This topic has been closed for replies.
Correct answer Dan Ebberts

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]];
}

2 replies

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
April 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]];
}
Overit-6Author
Participant
April 26, 2024

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

Dan Ebberts
Community Expert
Community Expert
April 26, 2024

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
Mylenium
Legend
April 26, 2024

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

 

Mylenium