How to stop character from moving.

I have a character model that has a character controller and rigid body. I’m moving the character by charactercontroller.move, I was wondering how can i stop the character from moving. Here’s my code.

Code for moving the object:

void MoveTo() //function for moving
	{
        Vector3 dir = targetPosition - transform.position;
		//Vector3 dir = new Vector3(targetPosition.x, targetPosition.y, targetPosition.z) - transform.position;
	    float dist = dir.magnitude;		
	    float move = speed * Time.deltaTime;
	    if(dist > move)
		{
			cc.Move(dir.normalized * move);
		    //transform.position += dir.normalized * move; //>>OLD Code
	    } 
		else 
		{
			cc.Move(dir);
		    //transform.position = targetPosition; //>>OLD Code
			anim.SetFloat(hash.moveSpeedFloat, 0f);  // sets the speed variable in the animator
	    }
		//Movement
	    //transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime; //>>OLD Code
		//Rotation Smooth
		transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * rotateSpeed);	 				
	}

Code for collision detection:

void OnControllerColliderHit(ControllerColliderHit hit)
	{
		if(hit.gameObject.tag == Tags.enemy) //if the character controller hits the enemy
		{			
			anim.SetFloat(hash.moveSpeedFloat, 0f);
		}
	}

As you can see I’m just setting variables for the animation to stop in the OnControllerColliderHit function. I was wondering if there’s a way to stop the moving function when the collider hits the enemy.

Thank you in advance.

It should be fairly simple, as your not working with adding force or any actually physics its just a matter of stopping your moveto function. So whereever you’re calling that functions have a bool that is turned on/off and in your collision function just change that bool.

What about a Boolean variable? Call it something like canMove and set it to true:

private bool canMove = true;

Make it so that at the start of your movement script, create an If statement around everything that is simply:

if(canMove = true){
    //Movement code here
}

And then just set your variable to false on collision.

So your whole script would look like this:

void MoveTo() //function for moving
    {
        If(canMove = true){
        Vector3 dir = targetPosition - transform.position;
       //Vector3 dir = new Vector3(targetPosition.x, targetPosition.y, targetPosition.z) - transform.position;
        float dist = dir.magnitude;       
        float move = speed * Time.deltaTime;
        if(dist > move)
       {
         cc.Move(dir.normalized * move);
           //transform.position += dir.normalized * move; //>>OLD Code
        } 
       else 
       {
         cc.Move(dir);
           //transform.position = targetPosition; //>>OLD Code
         anim.SetFloat(hash.moveSpeedFloat, 0f);  // sets the speed variable in the animator
        }
       //Movement
        //transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime; //>>OLD Code
       //Rotation Smooth
       transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * rotateSpeed);        
         }      
    }

Hope this helped!