Enemies are moving aroung wildly

Hey I’m programming a shooter game, where the characters can only move to the x and z axis. So after a while, I think mostly when the enemies are respawned, the enemies are moving around wildly to the x and z axis. It is unintentionally. The Enemies have a Rigidbody. So don’t know how to fix it. I’m using the ml-agent script so the code looks like this:

Vector3 dirToGo = Vector3.zero;
        Vector3 rotateDir = Vector3.zero;
    
            int action = Mathf.FloorToInt(act[0]);
    
            // Goalies and Strikers have slightly different action spaces.
            switch (action)
            {
                case 1:
                    dirToGo = transform.forward * 1f;
                    break;
                case 2:
                    dirToGo = transform.forward * -1f;
                    break;
                case 3:
                    rotateDir = transform.up * 1f;
                    break;
                case 4:
                    rotateDir = transform.up * -1f;
                    break;
                case 5:
                    dirToGo = transform.right * -0.75f;
                    break;
                case 6:
                    dirToGo = transform.right * 0.75f;
                    break;
            }
            transform.Rotate(rotateDir, Time.fixedDeltaTime * 200f);
            agentRB.AddForce(dirToGo * academy.agentRunSpeed, ForceMode.VelocityChange);

Where are you running this? Custom method, Update, Fixed?