Need Help with Velocity

Hello!

I have a problem with velocity in 2D game.

So, I have this creature. He must go with arrows. Every arrow is an object. I wrote script, for example, for left arrow:

    void Update () {

        if (Input.GetKeyDown (KeyCode.Mouse0) && mouserl) {          
            if (TmpRange > 0) {                  
                newTo = barmaglot.transform.position.x - 1f; // - New Position of object 'barmaglot'
                Debug.Log (newTo);
              
                // - Animation
                anim.SetInteger ("Napr", 3);
                anim.SetFloat ("Speed", Mathf.Abs (1));
                // - And now - go!
                rb.velocity = new Vector2 (-1f, rb.velocity.y);
            }
        }

        if (barmaglot.transform.position.x < newTo) {
            rb.velocity = new Vector2 (0, 0);
            anim.SetInteger ("Napr", 0);
            anim.SetFloat ("Speed", 0);
        }
    }

Works great. But.

When I click on another arrow, it stops. Code of another arrows is clone of this, but with another parameters. So, I can go only in one direction.

But Why?) I can’t understand it.

I have an idea, that velocity can’t be negative. Maybe, I need expand the character around its axis before moving in the opposite direction?

you’ve not got any parameters in there… the code which checks what direction to go to or stop is hard coded to look at the x axis.

 newTo = barmaglot.transform.position.x - 1f; 

 if (barmaglot.transform.position.x < newTo) {

etc.

Hm… Why?

I take “barmaglot.transform.position.x”. It upgrades every moment.

But newTo upgrades only one time, when we click on arrow. When “barmaglot.transform.position.x” < newTo - our sprite reached the desired point. So we stops it. And it works good.

Parameters - I mean “newTo = barmaglot.transform.position.x- 1f;” or “newTo = barmaglot.transform.position.x + 1f;”, or “newTo = barmaglot.transform.position.y + 1f;” if it’s arrow Top. Etc.