Transform.position engine bug

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.

What is a “wrong value”? Please specify expectation and actual.

Almost guaranteed that this is NOT a bug. The transform system has been in place practically unchanged for nearly two decades.

Perhaps you got confused with scientific notation ie -1.2345e-6 ? If so, those e-5 are almost zero, more so the less the number after the e. The values you get from logging position may look different from what you see in the Inspector or debugger.

It’s easy to misinterpret hallucination as confidence.

A feature used by tens of thousands of developers, hundreds of times each day for many years, is highly unlikely to contain such an obvious bug that no one has noticed until now. Instead of assuming the feature is flawed, consider asking what you might be doing wrong or what you may have misunderstood. Provide actual code, describe what is happening, and explain how it differs from what you expected, using a specific example rather than a generic argument.

1 Like

Instead of “trying” you may wish to do some “debugging.”

You have transforms… at a minimum you should give them unique names and start printing them out.

As others note, this is 100% not going to be an “engine bug” but rather your own bug.

Sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is. I’ll guess that at least one of them is not the one you so ardently believe it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.