Most of the FixedUpdate()
function is used to calculate your dir
value. However, at the end there are comments that say that the code is updating the position, but they don't use the calculated dir
value. I can't test this, but it seems that you need to change your last line to this:
transform.position += dir * (2 * Time.deltaTime);
As it currently stands, this code ignores any position of the enemy or the player:
transform.position += transform.forward * (2 * Time.deltaTime); // 20 is speed
It simply tries to move the enemy "forward," which is defined (in the Unity documentation) as "The blue axis of the transform in world space."
If you have code to rotate the enemy in the direction of dir
, it seems like you need to move the enemy in that direction.