object follow player unity code example

Example 1: make an enemy go towards player unity

float speed = whatever;
Vector2 positionToMoveTo = (whatever, whatever);

void Update()
{
  transform.position = Vector2.MoveTowards(transform.position, positionToMoveTo, speed * Time.deltaTime);
}

Example 2: unity creating an enemy that follow player

public float speed; private Transform target; // Use this for initialization void Start () {     target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>(); }  // Update is called once per frame void Update () {          transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);      }