Skip to main content
Inspiring
July 17, 2023
Answered

Expression to rotate object along the path

  • July 17, 2023
  • 1 reply
  • 867 views

So basically what I want to make is expression that rotates object along the path. But instead of directly turning to path, it should round up to closest right angle. For example, I want to move object from the center to top right corner. If horizontal velocity is higher than vertical - object should face to the right. And if vertical velocity is higher - object should turn face up.

Can't figure out myself, would appreciate the help

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

That's pretty strange. I wouldn't expect velocity to be zero at the keyframes, but apparently it can be. I'm not sure what a general solution would be, but you could try this to fix your particular example:

t = Math.max(time,position.key(1).time + .001);
t = Math.min(t,position.key(position.numKeys).time - .001);
v = position.velocityAtTime(t);
if (length(v) < .001){
  v = position.velocityAtTime(t - .001);
}
if (Math.abs(v[0]) > Math.abs(v[1])){
	v[0] < 0 ?  180 : 0;
}else{
	v[1] < 0  ? -90 : 90;
}

 

1 reply

Dan Ebberts
Community Expert
July 17, 2023

Assuming the position is keyframed, something like this might work:

t = Math.max(time,position.key(1).time + .001);
t = Math.min(t,position.key(position.numKeys).time - .001);
v = position.velocityAtTime(t);
if (Math.abs(v[0]) > Math.abs(v[1])){
  v[0] < 0 ?  180 : 0;
}else{
  v[1] < 0  ? -90 : 90;
}
TimoishaaAuthor
Inspiring
July 17, 2023

Hey Dan! Thanks. I encountered a problem where on time where keyframe is located objects' rotation suddenly returning to the original rotation. I was trying to figure it out myself, but still no progress. Do you know what might be a solution for this?

   

Dan Ebberts
Dan EbbertsCorrect answer
Community Expert
July 17, 2023

That's pretty strange. I wouldn't expect velocity to be zero at the keyframes, but apparently it can be. I'm not sure what a general solution would be, but you could try this to fix your particular example:

t = Math.max(time,position.key(1).time + .001);
t = Math.min(t,position.key(position.numKeys).time - .001);
v = position.velocityAtTime(t);
if (length(v) < .001){
  v = position.velocityAtTime(t - .001);
}
if (Math.abs(v[0]) > Math.abs(v[1])){
	v[0] < 0 ?  180 : 0;
}else{
	v[1] < 0  ? -90 : 90;
}