unity – Why does switching from local to global rotation change the result of code?

I had this IK script I made a while ago from a tutorial, and I had a problem where the script would cause glitchy movement.

I was able to figure out the problem, which was that the target of the bone was a child object. So when I rotated the target, the bone with the IK script, which was the parent of the target, would rotate, which would cause the target to move as well, since it is a child object, which would cause the parent to move, and so on.

I was looking for a solution and discovered that if I switched the button above the hierarchy that says “local” to “global”, the problem would go away.

I understand global and local spaces and such, but why does this fix the problem?

The IK script I have is this:

{
public int chainLength = 2;

public Transform target;
public Transform pole;

public int iterations = 10;

public float marginOfError = 0.001f;

(Range(0, 1))
public float snapBackStrength = 1f;

protected float() bonesLength;
protected float completeLength;
protected Transform() bones;
protected Vector3() positions;
protected Vector3() startDirection;
protected Quaternion() startRotationBone;
protected Quaternion startRotationTarget;
protected Quaternion startRotationRoot;

public bool ikActive = false;

// Start is called before the first frame update
void Start()
{
    if (ikActive)
    {
        Init();
    }
}

// Update is called once per frame
void Update()
{
    
}

void LateUpdate()
{
    if (ikActive)
    {
        ResolveIK();
    }
}

void Init()
{
    bones = new Transform(chainLength + 1);
    positions = new Vector3(chainLength + 1);
    bonesLength = new float(chainLength);
    startDirection = new Vector3(chainLength + 1);
    startRotationBone = new Quaternion(chainLength + 1);

    startRotationTarget = target.rotation;
    completeLength = 0;

    var current = transform;
    for(var i = bones.Length - 1; i>= 0; i--)
    {
        bones(i) = current;
        startRotationBone(i) = current.rotation;

        if (i == bones.Length - 1)
        {
            startDirection(i) = target.position - current.position;
        }

        else
        {
            startDirection(i) = bones(i + 1).position - current.position;
            bonesLength(i) = (bones(i + 1).position - current.position).magnitude;
            completeLength += bonesLength(1);
        }

        current = current.parent;
    }
}

void ResolveIK()
{
    if (target == null)
    {
        return;
    }

    if (bonesLength.Length != chainLength)
    {
        Init();
    }

    for (int i = 0; i < bones.Length; i++)
    {
        positions(i) = bones(i).position;
    }

    var rootRotation = (bones(0).parent != null) ? bones(0).parent.rotation : Quaternion.identity;
    var rootRotationDifference = rootRotation * Quaternion.Inverse(startRotationRoot);
   
    if ((target.position - bones(0).position).magnitude >= completeLength)
    {
        var direction = (target.position - bones(0).position).normalized;

        for (int i = 1; i < positions.Length; i++)
        {
            positions(i) = positions(i - 1) + direction * bonesLength(i - 1);
        }
    }
      
    else
    {
        for (int i = 0; i < positions.Length - 1; i++)
        {
            positions(i + 1) = Vector3.Lerp(positions(i + 1), positions(i) + rootRotationDifference * startDirection(i), snapBackStrength);
        }

        for (int iteration = 0; iteration < iterations; iteration++)
        {
            for (int i = positions.Length - 1; i > 0; i--)
            {
                if (i == positions.Length - 1)
                {
                    positions(i) = target.position;
                }

                else
                {
                    positions(i) = positions(i + 1) + (positions(i) - positions(i + 1)).normalized * bonesLength(i);
                }
            }

            for (int i = 1; i < positions.Length; i++)
            {
                positions(i) = positions(i - 1) + (positions(i) - positions(i - 1)).normalized * bonesLength(i - 1);
            }

            if ((positions(positions.Length - 1)).magnitude < marginOfError)
            {
                break;
            }
        }
    }

    if (pole != null)
    {
        for(int i = 1; i < positions.Length - 1; i++)
        {
            var plane = new Plane(positions(i + 1) - positions(i - 1), positions(i - 1));
            var projectedPole = plane.ClosestPointOnPlane(pole.position);
            var projectedBone = plane.ClosestPointOnPlane(positions(i));
            var angle = Vector3.SignedAngle(projectedBone - positions(i - 1), projectedPole - positions(i -1), plane.normal);
            positions(i) = Quaternion.AngleAxis(angle, plane.normal) * (positions(i) - positions(i - 1) + positions(i - 1));
        }
    }

    for (int i = 0; i < positions.Length; i++)
    {
        if (i == positions.Length - 1)
        {
            bones(i).rotation = target.rotation * Quaternion.Inverse(startRotationTarget) * startRotationBone(i);
        }

        else
        {
            bones(i).rotation = Quaternion.FromToRotation(startDirection(i), positions(i + 1) - positions(i)) * startRotationBone(i);
        }

        bones(i).position = positions(i);
    }
}

}

Also, does switching to global have any side effects/unintended consequences?