How do I check if an Object isn't moving? (C#)

I’m currently making the AI for my game, or the prototype AI, and I’m using the mechanim animator to animate it, so I’m making a boolean to check if the opponent is moving or not, so the animator will know what animation to play.

I’m having a problem with that, I’m checking if the opponent isn’t moving by using a “previous position” value and the print function to check if it’s working.

It isn’t, even when I get to print both positions, and it’s exactly the same, it still recognises it as moving.

Here’s the code:

void Update () 
	{
		if (myTransform.position == previousPosition)
		{
			moving = false;
			print("IT ISNAE MOVIN!");
		}
		else
		{
			//moving = true;
			print("IT'S ALIVE!" + transform.position.ToString() + previousPosition.ToString());
		}
		Debug.DrawLine(target.transform.position, transform.position, Color.red);
		
		//Look at target
		if (gameObject.tag == "Kruncanite")
		{
			myTransform.localRotation = Quaternion.Slerp(myTransform.rotation, 
			Quaternion.LookRotation(new Vector3(target.position.x, 0, target.position.z) - 
			new Vector3 (myTransform.position.x, 0, myTransform.position.z)),	rotationSpeed * Time.deltaTime);
		}
		else
		{
		myTransform.localRotation = Quaternion.Slerp(myTransform.rotation, 
			Quaternion.LookRotation(target.position - 
			myTransform.position),	rotationSpeed * Time.deltaTime);
		}
		
		//Move towards target
		
		if(Vector3.Distance(transform.position,target.position) > followDistance)
		{
			myTransform.position += Vector3.forward * moveSpeed * Time.deltaTime;
		}
		
		if (gameObject.tag == "Kruncanite")
		{
		  kruncaniteAnimation.SetBool("Moving", moving);
		}
		previousPosition = myTransform.position;
	}

What am I doing wrong?

Well, you are changing the position right BEFORE the check:

    myTransform.position += Vector3.forward * moveSpeed * Time.deltaTime;

Unless “moveSpeed” equals zero, this changes your position ALWAYS;

   if (myTransform.position == previousPosition) 

is therefore always not met. Even if you are at the same position as the target, using this method will still move FORWARD.

You can add somethiong like this:

float followDistance = 1.0;

if(Vector3.Distance(transform.position,target.position) > followDistance)  //just add this line to your code 
   myTransform.position += Vector3.forward * moveSpeed * Time.deltaTime;
...etc

this way, once you are close enough, you stop moving

the point is you need to update the movement code so that the character actually CAN stop moving!