I am working on a project where I cannot alter the player character, and I cannot add anything to it. So I need to figure out a way to knock back the player when the player enters a collison.
I am having a lot of problems trying to figure this out because I can’t figure out how to get an object’s rigidbody that collides when it collides with the object that is supposed to knockback.
Here is the script I have so far
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Knockback : MonoBehaviour
{
public float force = 30;
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Player")
{
Vector3 dir = col.contacts[0].point - transform.position;
dir = -dir.normalized;
GetComponent<Rigidbody>().AddForce(dir * force);
}
}
}
What I want to happen, is for the player to collide with an object, and knock them back. I cannot attach anything to the player, or attach any part of the player to the script in any way.
Does anyone have an idea of how to do this?
anything will help. thank you