• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Apr 26, 2024 Apr 26, 2024

Copy link to clipboard

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

Translate

Translate

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 guidelines

correct answers 1 Correct answer

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

Votes

Translate

Translate
LEGEND ,
Apr 26, 2024 Apr 26, 2024

Copy link to clipboard

Copied

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

 

Mylenium

Votes

Translate

Translate

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

Copy link to clipboard

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

Votes

Translate

Translate

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

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

Copy link to clipboard

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

Votes

Translate

Translate

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 guidelines