I'm trying to make this saber spin from the center while constantly having the anchor point between the two points (Core Start and Core End):
I prefer having to run an expression to do this but if there's another way please let me know!
I'm using this expression: (It's placed in Transform > Anchor Point)
x1 = effect("Saber")("Core Start")[0]
x2 = effect("Saber")("Core Start")[1]
y1 = effect("Saber")("Core End")[0]
y2 = effect("Saber")("Core End")[1]
transform.anchorPoint([x1/y1], [x2/y2])
But there is an error:
undefined value used in expression (could be an out of range array subscript )
In expressions (and Javascript) you need to end each line with a semicolon. That's the first problem with the expression you're using.
But as it stands the expression is not going to give you the results you want. To find the centre of two points what you need is the average of those two points. The average of any n terms is the sum of all the terms divided by n. And because you can add points in expressions, and you can multiply and divide points by scalars (single values, like numbers), all you have to do is add the points and divide by 2.
And finally, you seem to have a misunderstanding of how expressions work. To set the value of a property with you need to write an expression that returns a value that matches the property. To return a value the expressions engine looks for a term that evaluates to something. The simplest expression you could write would be something like
[123,345]
That's a valid expression. Boring, but valid (I sometimes write static terms like this as expressions as a way of "locking" a property, because expressions override the property controls.
So you don't need, and it is incorrect to use, transform.anchorPoint()
on the last line. It's not a function so using it in the form you have will result in an error.
TL;DR
let startPt = effect("Saber")("Core Start"); //N.B. semicolons
let endPt = effect("Saber")("Core End");
(startPt + endPt)/2; //this line evaluates to an array
The let
at the start of the variable declarations confines the scope of the variable. It's not essential, but it's good practice.
You could be more terse and do it as a one-liner, at the expense of readability:
(effect("Saber")("Core Start") + effect("Saber")("Core End")) / 2
I'd suggest you go and look at some basic JS tutorials, as well as reading the docs about expressions.