Move 2D object by clicking (Using raycast) - need help!

Hello people. I am very new to Unity, and thought i would start out with a simple point and click adventure game. Here i tried to play with the raycasting to make my character move to where i click. It works if i just replace the position with the hit.point. But if i start use "MoveTowards(), it just moves a tiny bit, and not even close to my wantedPosition.

If you don’t understand my problem, just ask for further information.
I really hope you can help! Thank you.

 private void walk()
          {
       
        if (Input.GetMouseButtonDown(0) && mouseScript.myMouseState == mouseScript.mouseState.walk && walkSpace.canJoeWalkHere) // don't worry about this long if statement. It is true :)
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
           
            if (Physics.Raycast (ray, out hit, 1000.0f))
            {
                //
                float target = hit.point.x;
                /* TESTING - Don't worry about this:
                deltaPosition = hit.point.x - currentPosition.x;
                if (mouseScript.mouseTracker == false)
                 {
                     deltaPosition = -deltaPosition;
                 }
               */
                wantedPosition = new Vector3 (target,gameObject.transform.position.y,gameObject.transform.position.z); // I only want the player to move in the x-axis. Thats why this is set to the gameObjects positions in y and z.
                currentPosition = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);    
            }
            joeWalking = true;
            if (joeWalking)
            {
                transform.position = Vector3.MoveTowards(currentPosition, wantedPosition, Time.deltaTime*speed);
              if(currentPosition == wantedPosition)
                {
                    joeWalking = false;
                }
               
            }
           
        }

It looks like your movement code is inside the if statement for your mouse click. Your character will only move when the mouse is down, which is why he is only moving a little tiny bit at a time. Also, it may be safer to check the distance between your position and target position instead of “if (currentPosition == wantedPosition)”. Floating point inaccuracy could cause issues with that kind of check i believe.

private void walk()
    {
        if (Input.GetMouseButtonDown(0) && mouseScript.myMouseState == mouseScript.mouseState.walk
            && walkSpace.canJoeWalkHere)

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, 1000.0f))
            {
                float target = hit.point.x;
                wantedPosition = new Vector3(target, transform.position.y, transform.position.z);
                currentPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
            }
            joeWalking = true;
        }

        if (joeWalking)
        {
            transform.position = Vector3.MoveTowards(currentPosition, wantedPosition, Time.deltaTime * speed);
            float distSqr = (wantedPosition - transform.position).sqrMagnitude;
            if (distSqr < 0.1f) //Adjust the 0.1f value if needed until it feels right
            {
                joeWalking = false;
            }
        }

I am so sorry that I didn’t reply on your post!! It helped alot and I made it work, thanks to your post. When i start work i just go deeply into it, and forget about everything else. I will in the future remember to thank the people who help me.!

No problem, glad it help :slight_smile: