How to remove z axis control from script

Hey, I have a basic enemy that patrols between two or more points, my game is a side scroller and Id like not to be able to push him forward/backward on the Z axis, causing him to fall off the platform. heres the part of the script that controls patrolling;

    if( grounded )
    {
        if(CurrentWayPoint < WayPoint.length)
        {
            var target : Vector2 = WayPoint[CurrentWayPoint].position;
            var moveDirection : Vector2 = target - transform.position;
            var velocity = rigidbody.velocity;
            if(moveDirection.magnitude < 1)
            {
                CurrentWayPoint++;
            }
            else
            {
                velocity = moveDirection.normalized * Speed;
            }
        }
        else
        {
            if( loop )
            {
                CurrentWayPoint = 0;
            }
            else
            {
                velocity = Vector3.zero;
            }
        }
        rigidbody.velocity = velocity;
    }

this is in the Update function and 'grounded' is if it is falling or not. How and where would I put something like `transform.position.z = 0;` ???

In the editor select your prefab or gameobject enemy and under the inspector in your rigidbody component click on constraints. You can freeze rotation and position on all the Axis. You could also try adding transform.position.z = 0; to your update function like this :

function Update () {

      transform.position.z = 0;

      if( grounded )
       {
        if(CurrentWayPoint < WayPoint.length)
        {
            var target : Vector2 = WayPoint[CurrentWayPoint].position;
            var moveDirection : Vector2 = target - transform.position;
            var velocity = rigidbody.velocity;
            if(moveDirection.magnitude < 1)
            {
                CurrentWayPoint++;
            }
            else
            {
                velocity = moveDirection.normalized * Speed;
            }
        }
        else
        {
            if( loop )
            {
                CurrentWayPoint = 0;
            }
            else
            {
                velocity = Vector3.zero;
            }
        }
        rigidbody.velocity = velocity;
    }}