How to knockback an enemy when the player picks up a coin?

Hi I am making a 2D platformer game where the player is being chased by a wall that will kill them. For my first level the player must collect 10 coins to unlock the door at the end of the level. I want to make it so that when the player picks up a coin it will knockback the wall a certain amount. I tried finding a tutorial on it but had no luck finding one.

Hi, Anthony210. I thought about the solution and found two ways. Solution 1 is to use a rigidbody like this:

(you only need using UnityEngine)

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

public class PushEnemy : MonoBehaviour
{
    public Vector2 PushDireciton;
    //How strong the enemy will be knocked back
    public GameObject Wall;
    [SerializeField]// If you want to see the private field
    private Rigidbody2D WallRigidbody;
    // Start is called before the first frame update
    void Start()
    {
              WallRigidbody = Wall.GetComponent<Rigidbody2D>();  
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void OnCollisionEnter(Collision col)
    {
        //Or OnTriggerEnter it is up to you
        if (col.gameObject.tag == "coin" /*Replace coin with your tag. Define or pick one if you haven't any.*/)
        {
            //Knock the wall back
            WallRigidbody.AddForce(new Vector2(PushDireciton.x * Time.deltaTime, PushDireciton.y * Time.deltaTime));
        }
        
    }

the other way is to do it with transform.Translate (which is better in your case):

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

public class PushEnemy : MonoBehaviour
{
    public Vector2 PushDireciton;
    //How strong the enemy will be knocked back
    public GameObject Wall;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    private void OnCollisionEnter(Collision col)
    {
        //Or OnTriggerEnter it is up to you
        if (col.gameObject.tag == "coin" /*Replace coin with your tag. Define or pick one if you haven't any.*/)
        {
            Wall.transform.Translate(PushDireciton);
        }
        
    }
}

As ı said the second solution is better in your case. However the first solution also has some advantages:
-The wall movement would look more realistic as it uses rigidbody component.
-The movement would be smooth. On the other hand transform.Translate would make it look like it is teleporting. You may use a for loop if you want to use transform.Translate with smooth movement but personally, I would not recommend that.

After you assign it to your player (Both your player and coin must have a rigidbody to detect colision. If you don’t want to use two rigidboies, use OnTrigger Enter instead that still requires at least one rigidbody) you should be able to knock back the wall.

If you have any questions or any runtime errors please feel free to write and notify me with @. Hope this helps.

@New_Game_Ideas Do you have discord so I can show you the problem?