# how to make my enemy stop walking?

so im new to unity and I watched some tutorials, and at some point I kinda understood how it works, I think I know what the problem is, because the enemy is supposed to just move to the left and when it hits the player it should stop, and when the player leaves the path it should keep moving, but instead of pausing and then continue it teleports forward, and the more I wait before moving the further the enemy jumps

``````public class EnemyBehaviour : MonoBehaviour {
public float delta = 1.5f;
public float speed = 0.05f;
private Vector3 startPos;
public float Walk = 1f;
public float time = 100f;

void Start()
{
startPos = transform.position;
}

void Update()
{
if (Walk == 1f)
{
Vector3 v = startPos;
v.x -= delta * (Time.time * speed / 8);
transform.position = v;
}
}

public void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "player")
{
PlayerControll.hp -= 1f;
Walk = 0f;
}

}

public void OnCollisionExit(Collision col)
{
if (col.gameObject.name == "player")
{
print("No longer in contact with " + col.transform.name);
Walk = 1f;
}

}
``````

}

tbh I barely understand this code, Im pretty sure the problem is that when its supposed stop the “x” kinda keeps moving, and then when it should go it just jumps to that position

You’re calculating the enemy speed based off the time counting from when the app was launched. This is what Time.time does.

``````   v.x -= delta * (Time.time * speed / 8);
``````

So when the enemy stops, it doesn’t really stop, because once you tell it to begin moving again the enemy will calculate all the time that has passed since it had stopped.

This example should help:

`````` public float speed;

public bool move = true;

void Update() {
if (move) {
// the values in the brackets are for " x, y, z "

transform.Translate(new Vector3(0, 0, 1) * speed * Time.deltaTime);
}
}
}
``````

Just toggle the move boolean variable if you want the enemy to move or stop.

`````` public void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "player")
{
move = false;
}

}

public void OnCollisionExit(Collision col)
{
if (col.gameObject.name == "player")
{
move = true;
}
}
``````

Just so it isn’t confusing. Time.deltaTime is not the same as Time.time. deltaTime calculates the time between frames while you are running stuff in the update() function.

On a faster or slower machine the update() will get called more or less, so in order to keep objects moving at the same rate on all machines, we use Time.deltaTime (the time in seconds it took to complete the last frame) and multiply this by the speed.