Weird movement behaviour

I created a Coroutine so that I could mimic the behavior of a gun slide going forwards and backwards when the gun is fired.

Here’s the Coroutine:

 IEnumerator MoveBackAndForth()
    {
        for(int i = 0; i < 10; i++)
        {
            transform.position += new Vector3(-0.01f, 0, 0);
            yield return 0;
        }

        for (int i = 0; i < 10; i++)
        {
            transform.position += new Vector3(0.01f, 0, 0);
            yield return 0;
        }
    }

When I fire the gun and call the Coroutine the way the slider behaves depends on where I point the gun in the World. In one position I’ll get the correct behavior and in others it’ll move up and down or some other way.

Could anyone help?

That’s because you use transform.position while you should use transform.localPosition :wink: