Apply explosive force to player at point of contact

Afternoon,

I’m trying to make a player get thrown back when they collide with a flat energy barrier. They’re colliding successfully and the enemies react appropriately but the player doesn’t seem to be affected by the explosive force.

Any help is appreciated!

public Rigidbody playerBody;
public Rigidbody enemyBody;

public float nudgeForce = 100f;
public float knockMultiplier = 2f;    
public float reactionRadius = 2f;

public Vector3 forceOrigin;

void Start() 
{
    playerMove = GameObject.FindWithTag("Player").GetComponent<PlayerMove>();
    healthScript = GameObject.FindWithTag("Player").GetComponent<PlayerHealth>();
    playerBody = GameObject.FindWithTag("Player").GetComponent<Rigidbody>();   
}

void OnCollisionEnter(Collision other) 
{
    forceOrigin = other.contacts[0].point;

    if (other.gameObject.CompareTag("Player"))
    {
        playerBody.AddExplosionForce(nudgeForce * knockMultiplier, forceOrigin, reactionRadius, 0, ForceMode.Impulse);
        //StartCoroutine(healthScript.MakeInvulnerable());
    }
    else if (other.gameObject.CompareTag("Enemy"))
    {
        enemyBody = other.gameObject.GetComponent<Rigidbody>();
        enemyBody.AddExplosionForce(nudgeForce * knockMultiplier, forceOrigin, reactionRadius, 0, ForceMode.Impulse);
    }    
}

}

Hi,

  • Check if player has right tag
  • Use Debug.log to see if it goes in the branch where you add forces
  • Try to increase forces, maybe they are too small

You’re adding a rigidbody to the enemys before you add the explosion force which seems to work. Your not doing that with the player though. Maybe try adding the rigidbody to the player of if the player already has a rigidbody chekc its not in kinematic mode.

I’ve tested your code. Indeed, the player pushes off intermittently, not always. The following code works better in my opinion. In it, I used not the first point of contact, but the arithmetic mean of their coordinate, so that the point of impact on the wall was more accurate. Also I use AddForce() instead of AddExplosionForce(), as the behavior of AddForce is more predictable for me.

void OnCollisionEnter (Collision other)
{
    forceOrigin = Vector3.zero;
    for (int i = 0; i < other.contacts.Length; i++)
    {
        forceOrigin += other.contacts [0] .point;
    }
    forceOrigin /= other.contacts.Length;

    if (other.gameObject.CompareTag ("Player"))
    {
        playerBody.AddForce ((playerBody.position - forceOrigin).normalized * nudgeForce * knockMultiplier, ForceMode.VelocityChange);
    }
    else if (…)
    {
    …
    }
}

_
EDITED: @PlasticFrames, I decided to approach the question from the player’s perspective. After all, through its OnCollisionEnter (), we can get the normal of the surface we are in contact with. Accordingly, we will know where to push the player away.
_
Here is a simple player script that can walk right-left-forward-backward and will push off objects with the TagOfEnergyBarrier tag (you can choose any tag and assign it to your Energy Barrier):

using UnityEngine;

public class MovePlayer : MonoBehaviour{
    Rigidbody rb;
    void Start(){
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate(){
        var move = transform.position + new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")) * 0.1f;
        rb.MovePosition(move);
    }

    // that's what you need
    private void OnCollisionEnter(Collision collision){
        if (collision.gameObject.CompareTag("TagOfEnergyBarrier"))
        {
            rb.AddForce(collision.GetContact(0).normal * 10, ForceMode.Impulse);
        }
    }
}