Enemy pushes player across screen

Hi,

I have my enemy to move to my players position when in range.
However, when the enemy collides with my player it doesn’t stop moving, it pushes the player across the screen.

This is a 2D game so can’t use navmesh, which is super easy.

I have a box collider for both of them to stop them passing through eachother and a circle collider which is a trigger.

All it is, is:

transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);

Any suggestions as to how I can do it so the enemy doesn’t continuously push the player across the screen?

Try this - you may have to customize it slightly to fit your game.
Make sure you have these up the top of your code

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

Here is the code - CSharp

public GameObject enemy;
public GameObject player;   

void OnTriggerEnter (Collider other) {

        if(other.gameObject.tag == "Player")
        {
                        enemy.transform.position = (X, Y, Z);
                        player.transform.position = (X, Y, Z);
        }
   
    }

Replace X, Y, and Z with the position of your objects.

Why do I need to put using UnityEngine.UI?

Also when the enemy comes to me, I don’t need my players position to move. I want him to stay still unless the user moves him.

I cant set x,y,z values for what you gave me. It only lets me set one value.