I’m using a script that adds force to the player when in collision with an enemy, my problem however is that the force gets added to the enemy instead of the player.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bounce : MonoBehaviour
{
void OnCollisionEnter(Collision c)
{
// force is how forcefully we will push the player away from the enemy.
float force = 1000;
// If the object we hit is the enemy
if (c.gameObject.tag == "Thump")
{
print("Hit");
// Calculate Angle Between the collision point and the player
Vector3 dir = c.contacts[0].point - transform.position;
// We then get the opposite (-Vector3) and normalize it
dir = -dir.normalized;
// And finally we add force in the direction of dir and multiply it by force.
// This will push back the player
print("Step 2");
GetComponent<Rigidbody>().AddForce(dir * force);
}
}
}