Ways to Unity 14: Push the enemy away from you by magic force if it collides with you

Basic idea

We just need to do a vector calculation to get a direction from you to the enemy, then apply a force to your enemy. It should be done in this way easily.

yingshaoxo: In the real life, I may not have superpowers. But in the virtual world, I got the full power of magic.

For the UI part

I assume you have added a tag called Enemy to your enemy game object.

The code

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

public class PlayerController : MonoBehaviour
{
    void Start()
    {
    }

    void Update()
    {
    }

    private void OnCollisionEnter(Collision other) {
        if (other.gameObject.CompareTag("Enemy")) {
            Vector3 awayFromPlayer = other.transform.position - transform.position;
            other.rigidbody.AddForce(awayFromPlayer*15, ForceMode.Impulse);
        } 
    }
}

Who writes this? 

https://yingshaoxo.blogspot.com/2020/12/ways-to-unity-14-push-enemy-away-from.html