How to slowly move one object towards another?

I’m trying to make a super simple topdown shooter game. I’m making the enemies slowly move towards you as you try to fight them off. Right now, I’ve just take the position of the player, then if the enemies x pos is less/greater than the players, increase/decrease it until they are the same. Same goes for y and z. For some reason I’m getting an error saying it expected a } after the first { in the update function, even though its matched at the end, after all the ifs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyAI : MonoBehaviour
{
    public Transform playerPos;
    public float enemySpeed;

    // Update is called once per frame
    void Update()
    {
        public float playerXPos = playerPos.position.x;
        public float playerYPos = playerPos.position.y;
        public float playerZPos = playerPos.position.z;

        if(playerXPos < transform.position.x){
        	transform.position.x = transform.position.x - enemySpeed;
        }
        if(playerYPos < transform.position.y){
        	transform.position.y = transform.position.y - enemySpeed;
        }
        if(playerZPos < transform.position.z){
        	transform.position.z = transform.position.z - enemySpeed;
        }
    }
}

The error comes because you declare the playerPos variables as public - variables defined within the scope of a function cannot have access modifiers, so removing the ‘public’ keyword for each of those would solve the error.

However, currently your code only works if the value for each axis is larger than that of the player, i.e. in one direction. Not only that, but it is a fairly convoluted way of doing this. Ideally, we would want to do the movement for all axes at once, which is where vectors come in;

 public class EnemyAI : MonoBehaviour
 {
     public Transform playerPos;
     public float enemySpeed;
 
     void Update()
     {
         //Find the direction of the player from the enemy (which is then normalized so that the vector doesn't get bigger with distance)
         Vector3 playerDir = (playerPos.position - transform.position).normalized;
         //Then, move the enemy in the direction of the player scaled by enemySpeed. Also multiply by Time.deltaTime because Update() is framerate dependant and we want the enemy to move at a constant speed regardless of performance
         transform.position += playerDir * enemySpeed * Time.deltaTime;
     }
 }