I I have a 2D topdown game and what I want to happen is that when an enemy hits a player that it was following, it gets knocked back for a sec, so that the enemy isn’t constantly hitting the player. Here is my script for moving the enemy
public float speedMax;
private float speed;
private Rigidbody2D rb2D;
private Transform target;
void Start()
{
speed = speedMax;
rb2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
target = GameObject.Find("Player").transform;
rb2D.MovePosition(Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime));
}
So what I want to know is how to I make the enemy move back a little when it hits the player, stop for less than a second than follow the player again. Almost like if a ball trying to go through a wall but when it gets hit it just bounces back and then goes forward again. Thanks for any help