Moving Back and Forth - Animation Node(PYTHON) vs Unity (C-SHARP) Boolean Switch Logic public class ChangeDirection : MonoBehaviour {
public bool movePositive = true;

// Update is called once per frame
void Update () {
    // CHECK IF MAX REACHED AND MOVING POSITIVE DIRECTION
    if (this.transform.position.y &lt;= 4f &amp;&amp; movePositive) {
        // MOVES IN POSITIVE DIRECTION
        this.transform.position += Vector3.up * Time.deltaTime * 5f;
    } 
    else {
        movePositive = false;
    }
    // CHECK IF MIN REACHED AND MOVING NEGATIVE DIRECTION

    if (this.transform.position.y &gt;= -4f &amp;&amp; !movePositive) {

        // MOVES IN NEGATIVE DIRECTION
        this.transform.position += Vector3.down * Time.deltaTime * 5f;
    } 
    else {
        movePositive = true;
    }
 }
}


NOT C-SHARP QUESTION EXACTLY - I NEED TO IMPLEMENT A BOOL LOGIC IN PYTHON
The code above moves an object back and forth within specified limit making one bool switching True and False,
Hits the max and moves in negative direction and hits the min and moves in positive direction. 

But as you can see in the image, I can't do the same thing with the same logic. How can it be implemented in Python script with Animaton Node - Blender?



Adding the code extra if you have problem with image

move_pos = True

if input_x &lt; 20 and move_pos:
    input_x += 1
else:
    move_pos = False

if input_x &gt; -20 and not move_pos:
    input_x -= 1
else:
    move_pos = True

output_x = input_x
I see no good reason to discretize such simple animation. A parametric approach might be better. To move an object between 0 and 1 in 20 frames, you can use such node tree:



To see why it works, plot the function. Another approach would be to use the animation nodes in Animation Nodes: