I’ve been trying like crazy to make this work:
Vector3 toNextJoint = jointObjects[i + 1].transform.position - jointObjects[i].transform.position;
Is not even complicated, but the engine keeps giving me a wrong value for jointObjects[i + 1].
How can I be so sure it doesn’t work?
Simply put, testObject.transform.position = jointObjects[i + 1].transform.position;// is giving me the wrong value.
And no, I am not confusing local and global spaces. I know that transform.position is global.
Is important to note that [i + 1] is the child of [i], but that doesn’t change much. It should still work! Seriously, the global position and the transform.position simply don’t match.
Another important thing to note is that this doesn’t happen always, but with concrete rotations on several axes, which makes me think this is a Unity bug.
EDIT: thanks for your replies. I just confirmed that it was my fault. I was simply not updating the value correctly, so it was giving me the previous position, but I confused it with a bug because it happened on very specific rotations (between other reasons). Anyway, could you guys help me with my actual problem?
I want to do this:
Vector3 toTarget = target.transform.position - jointObjects[0].transform.position;
toTarget /= toTarget.magnitude;
for (int i = 0; i < jointObjects.Length - 1; i++)
{
jointObjects[i + 1].transform.position = jointObjects[i].transform.position + toTarget * jointLength[i];
}
but with rotations. Basically align the next joint with the target in 3D space.
I’ve also tried using lookRotation, but that by default aligns the Z axis, not the toNextJoint axis. I tried giving it an offset. I don’t remember the result, but is safe to say that it didn’t work. Here’s my closest working code:
for (int i = 0; i < jointObjects.Length - 1; i++)
{
Vector3 toTarget = target.transform.position - jointObjects[i].transform.position;
Vector3 toNextJoint = jointObjects[i + 1].transform.position - jointObjects[i].transform.position;
Vector3 rotationAxis = Vector3.Cross(toNextJoint, toTarget).normalized;
float signedAngle = Vector3.SignedAngle(toNextJoint, toTarget, rotationAxis);
Vector3 currentLocalForward = toNextJoint.normalized;
Vector3 targetForward = toTarget.normalized;
//Here I've tried many things: for example, jointObjects[i].transform.rotation *= Quaternion.AngleAxis(signedAngle, rotationAxis);
//also jointObjects[i].transform.rotation *= Quaternion.FromToRotation(currentLocalForward, targetForward);
And nothing works. Actually, I am trying to do Inverse Kinematics and this is just the first step. I know there’s a default asset for that (and please correct me if I am wrong… again), but I need more control over some aspects, like clamping the min/max angle rotations and that asset doesn’t allow for that.
Thanks for reading all of this.