rigidbody.AddForce not working

I’m trying to apply a knockback to the player after he is hit by the enemy. They’re both rigidbodies so I thought it shouldn’t be that hard. For debug purposes I’ve set it so it does it everytime they collide.

Here’s the code:

void OnTriggerEnter (Collider other)
{
// If the entering collider is the player…
if(other.gameObject == player)
{
this.rigidbody.AddForce(target.rigidbody.velocity * force);
// … the player is in range.
playerInRange = true;

}
}

Force is set to be a public int and is set to 200 atm in unity.

Nothing happens. I’m sure the colliders are setup right because playerInRange gets set to true.

What could be the problem?

Quick property accessors were removed. You have to use GetComponent now. The tutorials haven’t been updated yet.

this.GetComponent<Rigidbody>().AddForce(target.GetComponent<Rigidbody>().velocity * force);
1 Like

Thanks for the reply. I’ve got an older version of Unity so it’s probably based on the old one?

I’ve tried both and they still dont work unfortunately.

Both technically work on pre-5.0. I’ve just gotten so used to correcting that problem that I completely failed to realize it wasn’t the problem you were having. :stuck_out_tongue: