How to make MoveTowards() stop using raycast?

Hey guys I wanted to ask about how to stop a movement of an object when the declaration of MoveTowards() is happening. For now I use the logic of when Vector3.Distance(obj.position, water.position) > stopdistance it will stop, then it will reduce the stopdistance so that the object will move again. But using this logic, the object can still move bit by bit into the water if the Player continuously spam into the direction of the water. I wanted to stop just by the water and still can move outside the water freely. I’ve been told it can be by using raycast, but confused on what to do.

Here’s my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Vector : MonoBehaviour
{
    public static Vector instance;
    public Transform obj;
    public Transform water;
    public float speed;
    private RaycastHit hit;
    Vector3 previousPosition;
    Vector3 targetPosition;
    float lerpMoving = 1f;
    public List<Transform> selectedUnit = new List<Transform>();
    private Vector3 mousePos;
    public bool insidetrigger = false;
    public float stopdistance = 10f;
    public bool onwater = false;
    public GameObject tidakDiAirUI;
    public bool canMove = true;
    public float jarakStopYgHilang;

    // Start is called before the first frame update
    void Start()
    {
        instance = this;
    }

    void Awake()
    {

    }

    // Update is called once per frame
    void Update()
    {
        // transform.position = obj.position;
        if(Vector3.Distance(obj.position, water.position) < stopdistance)
        {
            // stopdistance += 0.01f;
            onwater = false;
        }
        if(tidakDiAirUI.activeInHierarchy)
        {
            selectedUnit.Clear();
        }
        if (canMove == true)
        {
            HandleUnitMovement();
        }
    }

    public void HandleUnitMovement()
    {
        if (Input.GetMouseButtonDown(0))
            {
                mousePos = Input.mousePosition;
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
               
                if (Physics.Raycast(ray, out hit))
                {
                    LayerMask layerHit = hit.transform.gameObject.layer;

                    switch (layerHit.value)
                    {
                        case 8:
                            SelectUnit(hit.transform, Input.GetKey(KeyCode.LeftShift));
                            break;
                        case 10:
                            SelectUnit(hit.transform, Input.GetKey(KeyCode.LeftShift));
                            break;
                        default:
                            // isDragging = true;
                            DeselectUnit();
                            break;
                    }
                }
            }
       
        if (Input.GetKeyDown(KeyCode.Mouse1) && HaveSelectedUnit())
        {
            if (Input.GetMouseButtonDown(1) && selectedUnit.Contains(obj))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);         
                if (Physics.Raycast(ray, out hit))
                {
                    LayerMask layerHit = hit.transform.gameObject.layer;

                    switch (layerHit.value)
                    {
                        case 4:
                            break; 
                        case 8:
                            break;
                        case 9:
                            break;
                        case 10:
                            break;
                        default:
                            foreach(Transform unit in selectedUnit)
                            {
                                onwater = false;
                                previousPosition = transform.position;
                                targetPosition = new Vector3 (hit.point.x, previousPosition.y, hit.point.z);
                                lerpMoving = 0;
                            }
                            break;
                    }  
                }
            }
        }

        if(lerpMoving < 1)
        {
            movePlayer();  
        }
    }

    public void movePlayer()
    {
        if(!onwater)
        {
            if(Vector3.Distance(obj.position, water.position) > stopdistance)
            {
                lerpMoving += Time.deltaTime;
                transform.position = Vector3.MoveTowards(previousPosition, targetPosition,
                speed * lerpMoving);
            }
            else
            {
                onwater = true;
                stopdistance -= .01f;
            }
        }
    }
       
    private void SelectUnit(Transform unit, bool canMultiselect = false)
    {
        if(!canMultiselect)
        {
            DeselectUnit();
        }
        selectedUnit.Add(unit);
        unit.Find("Highlight").gameObject.SetActive(true);
    }

    private void DeselectUnit()
    {
        for (int i = 0; i < selectedUnit.Count; i++)
        {
            selectedUnit[i].Find("Highlight").gameObject.SetActive(false);
        }
        selectedUnit.Clear();

    }

    public void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "water")
        {
            InsideTrigger();
            // tidakDiAirUI.SetActive(true);
            // canMove = false;
            // gameObject.transform.Find("Highlight").gameObject.SetActive(false);
        }
    }
   
    public void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "water")
        {
            OutTrigger();
            jarakStopYgHilang = 10f - stopdistance;
            stopdistance = jarakStopYgHilang + stopdistance;
        }
    }

    public void MengertiTidakDiBisaDiAir()
    {
        tidakDiAirUI.SetActive(false);
        canMove = true;
    }

    public void InsideTrigger()
    {
        insidetrigger = true;
    }

    public void OutTrigger()
    {
        insidetrigger = false;
    }

    public void StopMove()
    {
        canMove = false;
    }

    public void StartMove()
    {
        canMove = true;
    }

    private bool HaveSelectedUnit()
    {
        if (selectedUnit.Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}

The code for the movement and stopping can be found on Update(), HandleUnitMovement(), movePlayer(), and OnTriggerExit()

Hi,

Perhaps it’s easiest to make a mesh with the same shape as the lake, and put a invisible mesh collider with no friction, and no bounce on it. Then the physics system will take care of it for you, preventing the player from walking into the lake like hitting a wall.

If the lake is a flat surface on the ground with another collider tag? Then perhaps you can use a couple of down rays to probe, to find the edge of the lake. Then zeroing out all player moving forces perpendicular to that edge, and just apply the remaining forces to the player.

Hi sorry for just replying now because I just logged in to Unity Forum today. Thank you for the advice, I will try it!!