It’s not clear from the question whether your problem is with your character or with your enemy, but after trying, probably both. It’s because the enemy moves “inside” your player, they are colliding and that leads to weird stuff.
a) One solution is that you set up a follow distance so the enemy doesn’t get too close to the player, something like this:
public float followDistance = 5f;
bool canMove = false;
...
void Update() {
// rotation can stay where it is
if (canMove)
transform.position += transform.forward * moveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, player.position) < followDistance)
canMove = false;
else
canMove = true;
}
b) Another solution which may be more sensible if your enemies do melee attack is to use OnCollisionEnter() and OnCollisionExit() and set in a boolean whether the enemy can or can’t make additional movement towards the player.
c) Use Trigger colliders on your enemies. You can combine this with solution b) and use OnTriggerEnter() and OnTriggerExit() instead.
Thanks buddy, but for the first solution there is a problem which is the followDistance variable can notbe substracted from transform.position because it is a vector and the other is just an integer