one walking script works the other incorporated into a state machine does not [solved]

here is the one that works:

public class HeroWalk : MonoBehaviour
    {
        [SerializeField]
        float moveSpeed = 10;

        //public Transform other;
        public Animator animator;
        //public Animation walkanimation;
        private Vector3 prevPos;
        private Vector3 velocity;

        Vector3 destination;
        //Vector3 right = new Vector3(1, 0, 0);

        int righthash = Animator.StringToHash("HeroMoveRight");
        int uphash = Animator.StringToHash("HeroMoveUp");
        int lefthash = Animator.StringToHash("HeroMoveLeft");
        int downhash = Animator.StringToHash("HeroMoveDown");
        int idlehash = Animator.StringToHash("HeroIdle");
void Start()
        {
            destination = transform.localPosition;
            prevPos = transform.localPosition;
        }

        void Update()
        {

            if (Input.GetMouseButtonDown(0))
            {
                destination = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                destination.z = transform.localPosition.z;
            }
            if (enemy_attacked.hero_stop == false)
                transform.localPosition = Vector3.MoveTowards(transform.localPosition, destination, moveSpeed * Time.deltaTime);
            //transform.localPosition = Vector3.Lerp(transform.localPosition, destination, moveSpeed * Time.deltaTime);

            // get squared speed to see if object is moving

            velocity = (transform.position - prevPos) / Time.deltaTime;

            // get direction
            //gets a vector that points from the player's position to the target's.
            var heading = destination - transform.position;
            var distance = heading.magnitude;
            var direction = heading / distance; // This is now the normalized direction.

            prevPos = transform.position;

            //print(velocity.sqrMagnitude);
            // set the animation trigger based off magnitude and direction
            if (velocity.sqrMagnitude > 0)
            {
                if (Vector3.Angle(Vector3.right, direction) >= 0 && Vector3.Angle(Vector3.right, direction) <= 45)
                {
                    animator.SetTrigger(righthash);
                }
                else if (Vector3.Angle(Vector3.up, direction) >= 0 && Vector3.Angle(Vector3.up, direction) < 45)
                {
                    animator.SetTrigger(uphash);
                }
                else if (Vector3.Angle(Vector3.left, direction) >= 0 && Vector3.Angle(Vector3.left, direction) <= 45)
                {
                    animator.SetTrigger(lefthash);
                }
                else if (Vector3.Angle(Vector3.down, direction) >= 0 && Vector3.Angle(Vector3.down, direction) < 45)
                {
                    animator.SetTrigger(downhash);
                }
            }
            else
                animator.SetTrigger(idlehash);
            //(1, 0), (0, 1), (-1, 0), (0, -1)
        }
    }

here is the one that does not:

public Animator animator;
      
        public enum State
        {
            Idle,
            WalkingToSpot,
            ApproachingEnemy,
            AttackingEnemy
        }

        public State state = State.Idle;

        public Vector3 targetPos;
        public GameObject enemy;    // (or some more specific type, Monster or whatever)
        public float walkSpeed = 10;

        //private Vector3 destination;
        private Vector3 prevPos;
        private Vector3 myVelocity;

        //private RaycastHit hit;

        //need to fix this?
        bool swinging = true;
        bool thrusting = false;

        public int damage_taken = 0;

        private float nextActionTime = 0.0f;
        public float period = 1.0f;

        private Boolean clicked = false;
        private Boolean enemy_clicked = false;
        private Boolean enemy_over = false;


        void Start()
        {
            prevPos = transform.localPosition;
            targetPos = transform.localPosition;
            targetPos.z = transform.localPosition.z;
        }
        // got to get state logic right.  try to work out each step.
        void Update()
        {//maybe have a timer on the "clicks"
            if (Input.GetMouseButtonDown(0))
            {
                CastRay();
                targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                clicked = true;
                if (enemy_over == false)
                {
                    Debug.Log("!Enemy_Over");
                    clicked = true;
                    enemy_clicked = false;
                    targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                }
                else if (enemy_over == true && InAttackRange(Input.mousePosition, 10))
                {
                    Debug.Log("Enemy_Clicked");
                    enemy_clicked = true;
                    clicked = false;
                    //enemy_over = false;
                }
                else
                {
                    Debug.Log("Clicked");
                    clicked = true;
                    enemy_clicked = false;
                    targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                }
                HandleClick(Input.mousePosition);
            }

            float a = targetPos.x + new Vector3(1, 1, 0).x;
            float a1 = targetPos.x - new Vector3(1, 1, 0).x;
            float b = targetPos.y - new Vector3(1, 1, 0).y;
            float b1 = targetPos.y - new Vector3(1, 1, 0).y;
            float c = targetPos.z - new Vector3(1, 1, 0).z;
            float c1 = targetPos.z - new Vector3(1, 1, 0).z;

            switch (state)
            {
                case State.Idle:
                    //Debug.Log("State.Idle");
                    WalkTowards(targetPos, animator); // should show the idle animation - should i do this in a separate function? probably
                    if (!((transform.position.x <= a) || (transform.position.x >= a1)) && ((transform.position.y <= b) || (transform.position.y >= b1)) && ((transform.position.z <= c) || (transform.position.z >= c1))) state = State.WalkingToSpot;
                    break;
                case State.WalkingToSpot:
                    Debug.Log("State.WalkingToSpot");
                    WalkTowards(targetPos, animator);
                    if (((transform.position.x <= a) || (transform.position.x >= a1)) && ((transform.position.y <= b) || (transform.position.y >= b1)) && ((transform.position.z <= c) || (transform.position.z >= c1))) state = State.Idle;
                    break;
                case State.ApproachingEnemy:
                    // if our enemy is gone, switch to the idle state
                    //Debug.Log("State.ApproachingEnemy");
                    if (enemy == null) state = State.Idle;
                    else if (InAttackRange(Input.mousePosition, 10) && enemy_clicked == true) state = State.AttackingEnemy;
                    else WalkTowards(enemy.transform.position, animator);
                    break;
                case State.AttackingEnemy:
                    //Debug.Log("State.AttackingEnemy");
                    if (enemy == null) state = State.Idle;
                    if (InAttackRange(Input.mousePosition, 10) && enemy_clicked == true) Attack(enemy);
                    break;
            }
        }

        void CastRay()
        {
            //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            var heading2 = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
            var distance2 = heading2.magnitude;
            var direction2 = heading2 / distance2;
            Debug.DrawLine(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition), Color.red, 10);
            //Debug.DrawRay(transform.position, direction2, Color.red);
            RaycastHit2D hit = Physics2D.Raycast(transform.position, direction2, 10);
            if (hit.collider != null && hit.collider.tag == "Enemy")
            {
                //Debug.Log(hit.collider.gameObject.enemy);
                enemy_over = true;
            }
        }

        void WalkTowards(Vector3 destination, Animator animator)
        {
            if (clicked == true)
            {
                Debug.Log("fcn_WalkTowards");
                // move towards our destination
                //destination = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                transform.localPosition = Vector3.MoveTowards(transform.localPosition,
                        destination, walkSpeed * Time.deltaTime);
                targetPos = destination;
                var heading = targetPos - transform.position;
                var distance = heading.magnitude;
                var direction = heading / distance; // This is now the normalized direction.

                // velocity to determine if moving or not
                myVelocity = (transform.position - prevPos) / Time.deltaTime;

                prevPos = transform.position;  //is this code right?  check

                // animate movement
                if (myVelocity.sqrMagnitude > 0)
                {
                    if (Vector3.Angle(Vector3.right, direction) >= 0 && Vector3.Angle(Vector3.right, direction) <= 45)
                    {
                        //animator.SetTrigger(righthash2);
                        animator.Play("HeroMoveRight2");
                    }
                    else if (Vector3.Angle(Vector3.up, direction) >= 0 && Vector3.Angle(Vector3.up, direction) < 45)
                    {
                        animator.Play("HeroMoveUp2");
                    }
                    else if (Vector3.Angle(Vector3.left, direction) >= 0 && Vector3.Angle(Vector3.left, direction) <= 45)
                    {
                        animator.Play("HeroMoveLeft2");
                    }
                    else if (Vector3.Angle(Vector3.down, direction) >= 0 && Vector3.Angle(Vector3.down, direction) < 45)
                    {
                        animator.Play("HeroMoveDown2");
                    }
                }
                else
                    animator.SetTrigger("HeroIdle2");
            }
        }


        bool InAttackRange(Vector3 clickPos, int distance)
        {
            // cast a ray, figure out what was hit, and then set
            // targetPos or enemy, and EnterState appropriately
            var heading3 = clickPos - transform.position;
            var distance3 = heading3.magnitude;
            var direction3 = heading3 / distance3;
            //RaycastHit2D hit = Physics2D.Raycast(transform.position, direction3);
            return (Physics2D.Raycast(transform.position, direction3, distance));
        }
        
        // get unstuck from loop
        void Attack(GameObject enemy1)
        {
            if (enemy_clicked == true)
            {
                if (Time.time > nextActionTime && InAttackRange(transform.position, 10) == false)
                {
                    nextActionTime = Time.time + period;
                    AttackEnemy(enemy1);
                }
            }
        }

        void AttackEnemy(GameObject enemy1)
        {
            float xDiff = enemy1.transform.position.x - transform.position.x;
            float yDiff = enemy1.transform.position.y - transform.position.y;
            double attackangle = Math.Atan2(yDiff, xDiff) * (180 / Math.PI);
            // animate
            Debug.Log("AttackEnemy");
            if (attackangle > 315 && attackangle <= 45)
            {
                Debug.Log("AttackEnemy");
                animator.Play("HALeft2");
            }
            else if (attackangle > 45 && attackangle <= 135)
            {
                animator.Play("HARight2");
            }
            else if (attackangle > 135 && attackangle <= 225)
            {
                animator.Play("HALeft2");
            }
            else if (attackangle > 225 && attackangle <= 315)
            {
                animator.Play("HADown2");
            }

            // wait and only attack once every certain amount of time
            attack_skill_hits attack = new attack_skill_hits();
            attack_skill_evades defend = new attack_skill_evades();
            swinging_thrusting damage = new swinging_thrusting();
            if (attack.skill_hits(7) && !defend.skill_evades(5, 7, 9))
            {
                if (swinging)
                {
                    damage_taken += damage.dmg_swinging(15) + 2;//+ weapon damage modifier + other modifiers(weapon specialization etc.);
                }
                else
                {
                    damage_taken += damage.dmg_thrusting(15) + 1;// + weapon damage modifier + other modifiers(weapon specialization etc.);
                }
                Debug.Log(damage_taken);
            }
            //animator.settrigger
        }
  
        void HandleClick(Vector3 clickPos)
        {
            //Ray ray = Camera.main.ScreenPointToRay(clickPos);
            var heading1 = clickPos - transform.position;
            var distance1 = heading1.magnitude;
            var direction1 = heading1 / distance1;
            Ray2D ray = new Ray2D(transform.position, direction1);
            //RaycastHit2D hit;
            RaycastHit2D hit = Physics2D.Raycast(transform.position, direction1);
            //Debug.DrawLine(transform.position, clickPos, Color.red, 10);
            //Debug.DrawRay(transform.position, direction1, Color.red);
            //if (Physics2D.Raycast(ray, out hit, 100))
            if (Physics2D.Raycast(transform.position, direction1, 10))
            {
                if (hit.collider != null && hit.collider.tag == "Enemy")
                {
                    state = State.ApproachingEnemy;
                    targetPos = clickPos;
                }
            }
            else
            {
                targetPos = clickPos;
                state = State.WalkingToSpot;
            }
        }

it’s the same code from the first one that works incorporated into the second one but movement doesn’t work with this second script. the hero object just floats off in random directions when i click. can anyone figure out what’s going wrong?

it’s been almost a week, so bump. got the raycasting figured out but movement is still buggy.

edit: you don’t have to look at the entirety of the second block of code, just the walking function named WalkTowards and the update function and the code at the top of the script.

edit: solved! it was really simple. i was forgetting one Camera.main.ScreenToWorldPoint() around my destination vector code. hooray for a simple answer to a 300 line block of messy code lol.