1
votes

I am programming a top-down hack-and-slash game for a class. We want a mechanic where if you get hit by an enemy, or an enemy hits you, you get 'knocked back'. Unfortunately, no matter what I have tried, I cannot get either the enemy or the player to react to the force.

Here is a list of things I have checked that have been suggested in other questions like this:

  • Player/Enemy is NOT Kinematic
  • Tried with both Gravity on and off
  • No positions/rotations are frozen
  • Player/Enemy have Rigidbodies attached
  • Player/Enemy have colliders attached, one with and one without the 'isTrigger' function checked.
  • Tried both OnCollisionEnter and OnTriggerEnter
  • The force value is high, the mass, drag, and angular drag are low

I have run out of ideas. Any and all support you can give is greatly appreciated.

Here is a snippet of the code from a script from the player Object:

 public void OnTriggerEnter(Collider col)
{
    if (col.gameObject.tag == "EnemyHit" && !invincible)
    {
        Debug.Log("The player has been hit!");

        //sets player as invincible
        invincible = true;

        // Set the damaged flag so the screen will flash.
        hit = true;

        timeOfHit = Time.time;

        // Reduce the current health by the damage amount.
        currentHealth -= 1;

        GetComponent<Rigidbody>().AddForce(transform.forward * recoilThrust, ForceMode.Force);
        Debug.Log("This is the force that is being added to the player when it is hit. : " + -transform.forward * recoilThrust);
         //...
         }
 }

I can prove (using the Debug.Log) function, that the code reaches there, and the force is calculated.

2
Hmmm, nothing immediately seems wrong here, since you mention that the collision is detected at the end of your post. How are you moving your player around? Is it physics-based, or does it directly modify the transform position? - Serlite
Could you provide some values? For the calculated force and mass? Also a screenshot of the rigidbody to see the stats there. - Gunnar B.
@Programmer - I will see what I can do. Most of the code has been documented to contain the programmer's names, so I will see if I can take them out for a version of the build without people's names. Serlite- I am using CharacterController.Move(). GunnarB.- The player's mass, drag, and angular drag are at 1. The enemy's mass, drag, and angular drag are at 100. When turned down to 1, the enemy moves away. Problem still with player. And the message I got is: This is the force that is being added to the player when it is hit. : (4542.7, 1.4, -99896.8) Thank you all for your help so far. - user3280790
Hmm, that is a lot of force so that should not be a problem. One thing you could try is to use another ForceMode. Probably Impulse. - Gunnar B.
Tried it before, did nothing. Working on a 'build' of the project sans people's names right now, should be up relatively soon. - user3280790

2 Answers

0
votes

To summarise the comments:

Rigidbody.AddForce doesn't work when the object has a CharacterController besides the rigidbody. In this case the effect has to be "faked". Possible ways can be found here:

Basically you need to apply the force using CharacterController.Move.

0
votes

Is Kinematic enabled? Doing so will make the object ignore physic forces.