Activity › Forums › Adobe After Effects Expressions › 2D LookAt expression doesn’t wanna work
-
2D LookAt expression doesn’t wanna work
Posted by Nick Kirastoulis on June 19, 2011 at 8:47 pmI’ve used the 2d lookat expression before and it worked with no problems. Now, i’m trying to do it again but it gives me an error every time.
here’s what i did:
i made a new comp and created 2 null objects (2D).
i kept one of the nulls in the center and moved the other null in some random part of the composition.
what i want to do is to make the rotation of the center null follow the position of the other null but it doesn’t want to cooperate.
can anyone help me with this issue?
Konstantin Kartashov replied 14 years, 8 months ago 3 Members · 6 Replies -
6 Replies
-
Lukas Thorsson
June 20, 2011 at 11:53 amUse this simple Script:
pointA = thisLayer;
pointB = thisComp.layer("Null 2");a = pointA.position[0] - pointB.position[0];
b = pointA.position[1] - pointB.position[1];
switcher = 0;if (b < 0) {switcher = -180};
if (b == 0) {degree = 90} else {degree = -radiansToDegrees(Math.atan(a/b))}degree + value + switcher
The difference between Point a and b can be described as a triangle. The difference on X Axis is line segment a, Y Axis is line segment b, line segemnt c would be the distance, but we only need a and b.
Computing the Angle beween is arctan(a/b). As in https://en.wikipedia.org/wiki/Triangle#Computing_the_sides_and_angles described.
If the difference on y Axis is negative the Triangle “flips”, so we need to rotate the value by 180 degrees. If its zero, we get an divide by zero error, so we’re catching this case also.
By Adding the “value” Argument at the End you can easily rotate your layer to the right direction.
-
Konstantin Kartashov
August 16, 2011 at 12:02 pmHi, Lukas,
it`s a great stuff, thank you. But is it possible to convert these all to screen coordinates? just to make the look at rotation independent from parent object`s rotation. -
Lukas Thorsson
August 24, 2011 at 3:06 pmHi Konstantin,
try replacing the first 2 lines with
pointALayer = thisLayer;
pointA = pointALayer.toComp(pointALayer.anchorPoint);pointBLayer = thisComp.layer("Null 3");
pointB = pointBLayer.toComp(pointBLayer.anchorPoint);
Bye
Lukas
-
Konstantin Kartashov
August 30, 2011 at 11:19 amsorry Lukas, but it doesn`t work. It returns the error message “Class `array` has no property or method named `position`”. Could you please suggest any other solutions?
-
Lukas Thorsson
August 30, 2011 at 12:23 pmOoops… I was a bit in a hurry when i made the post…
pointALayer = thisLayer;
pointA = pointALayer.toComp(pointALayer.anchorPoint);pointBLayer = thisComp.layer("Null 3");
pointB = pointBLayer.toComp(pointBLayer.anchorPoint);a = pointA[0] - pointB[0];
b = pointA[1] - pointB[1];
switcher = 0;
if (b < 0) {switcher = -180};
if (b == 0) {degree = 90} else {degree = -radiansToDegrees(Math.atan(a/b))}degree + value + switcher
Same script but without .position @ line 6 and 7. In the first script Variable pointA was a Layer, so pointA.position referred to the layers position. Now we calculate the position using the toComp() Function and save the position to the Variable pointA.
-
Konstantin Kartashov
August 31, 2011 at 3:02 pmThank you very very much. Now it`s exactly what i wanted to get.
Reply to this Discussion! Login or Sign Up