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

# Expression to detect horizontal axis vector

Explorer ,
Nov 30, 2022 Nov 30, 2022

Copied

Hey guys. I'm trying to make an object that is gonna horizontally flip itself when it changes his x axis vector.

It basically works, but the problem here that when object isn't moving anywhere or hit keyframe, velocity goes to 0 and because there is no statements that are going to flip object scale, so it goes to the start scale of [100, 100]. Numbers on screenshot is velocity - when it's negative - scale flips from [100, 100] to [-100, 100]
There is expression that I've been using;

```if(transform.position.velocity[0]>0){
[-100, 100]
}else{
[100,100]
}```

What I want to do is the object will remember his last scale number. So if we go from left to right, animation ends - object gonna safe his last scale. What do you think?

TOPICS
Error or problem , Expressions

Views

308

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 , Nov 30, 2022 Nov 30, 2022

Something like this, probably:

``````if(transform.position.velocity[0]>0){
[-100, 100]
}else if (transform.position.velocity[0]<0){
[100,100]
}else{
t = time - thisComp.frameDuration;
gt = true;
while(t >= inPoint){
if (transform.position.velocityAtTime(t)[0] > 0){
break;
}else if (transform.position.velocityAtTime(t)[0] < 0){
gt = false;
break;
}
t -= thisComp.frameDuration
}
gt ? [-100,100] : [100,100]
}``````

6 Replies 6
LEGEND ,
Nov 30, 2022 Nov 30, 2022

Copied

It's all explained here:

http://www.motionscript.com/articles/speed-control.html

You need to integrate the values in a loop and then make determinations about the values based on some threshold.

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
Explorer ,
Nov 30, 2022 Nov 30, 2022

Copied

Thank you for you reply, Mylenium! That's a beatiful article, I'll be glad to read it later. But I didn't quite understand how I can use this for my purpose? There is too many for me to understand what and where I can use it right now

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 ,
Nov 30, 2022 Nov 30, 2022

Copied

Something like this, probably:

``````if(transform.position.velocity[0]>0){
[-100, 100]
}else if (transform.position.velocity[0]<0){
[100,100]
}else{
t = time - thisComp.frameDuration;
gt = true;
while(t >= inPoint){
if (transform.position.velocityAtTime(t)[0] > 0){
break;
}else if (transform.position.velocityAtTime(t)[0] < 0){
gt = false;
break;
}
t -= thisComp.frameDuration
}
gt ? [-100,100] : [100,100]
}``````

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Nov 30, 2022 Nov 30, 2022

Copied

It's beautiful!! It works exactly how I wanted! Thank you, Dan.

I don't quite understand what is going on here, but I'll take a closer look 🙂

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 ,
Nov 30, 2022 Nov 30, 2022

Copied

If the horizontal velocity is zero at the current frame, it backs up, frame-by-frame, until it finds the most recent frame where the horizontal velocity was non-zero and uses that value.

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Nov 30, 2022 Nov 30, 2022

Copied

LATEST

Thanks a lot for your time Dan! I figured out how it works 🙂
It's actually pretty simple and clever when I started to realise how the whole script works