Player Knockback / PushBack

Can someone just point me in the right direction. I have my scene where the player walks through a trigger, there is a explosion.

Can someone tell me how would I cause the player to also be knocked or pushed back when they go to into the trigger?

I’m sure its probably something simple, I tried searching but only thing that comes up is about pushing a enemy back. And I can’t find anything in the Docs. I want to push the player back.

Thanks in advance for any help.

With the standard FPS controller, you may use a specific function in the CharacterMotor.js script, SetVelocity. You can put this script in the trigger object:

var velBack:float = 15;

function OnTriggerEnter(col:Collider){

	if (col.tag == "Player"){
		dir = (col.transform.position - transform.position).normalized;
		var charMotor = col.GetComponent(CharacterMotor);

It calculates the direction from the center of the trigger to the player, than apply a velocity defined by velBack. Notice that the character must be tagged Player for this to work.


There’s a little problem in the CharacterMotor script: it sends a message OnExternalVelocity whenever SetVelocity is called, what produces annoying warnings in the console view because there’s no OnExternalVelocity function to receive it. The easiest solution is to comment out or delete the SendMessage instruction in CharacterMotor.js (line 582, I guess) - it’s completely useless anyway.

If one doesn’t want to mess with this script, another solution is to add an empty function OnExternalVelocity to any other script attached to the First Person Controller:

function OnExternalVelocity(){

This way SendMessage will have a receiver and stop bothering us with stupid warnings.

If your player is a rigid body, all you have to do is apply a force to it in an opposite direction.

If your player is not a rigid body, it’s slightly more complicated if you want it to look at all realistic but the idea is essentially the same. Here are the steps you would need to take:

  • get the direction of the explosion’s force. I would do this by taking the player’s position vector and subtracting it from the explosion’s position vector. You will probably want to zero the Y values out of both of these vectors before you subtract them, since you’re likely going to want a planar vector (XZ) and handle the ‘height’ of the knockback yourself.
  • that should give you a vector that points in the direction you’re wanting your player to travel. From here you need a ‘movement speed’ variable. If you’ve ever implemented Jump functionality with a character controller, the concept is essentially the same.
  • Multiply your direction vector by your movement speed, and apply gravity each frame.
  • You may also want to apply an ‘air friction’ so that your player does not maintain a constant speed when he flies through the air. This is entirely optional though, obviously.
  • Once the character controller is grounded again, the ‘knock back’ is complete and you can probably just zero out the knock back vector, unless you want the character to slide a bit.