How do I push a character?

If you want to push or knock a character back, there’s a couple of options.

 

AddImpulse

With this approach, we just add an impulse to the Actor Controller. This typically doesn’t interrupt motions or animations as it just adds additional movement based on a pseudo force.


1
2
MotionController lMC = Component.FindObjectOfType();
lMC.ActorController.AddImpulse(lMC.Transform.forward * -20f);

 

Motions

Another option is to use my “Pushed Back” motion. This will apply a small animation as the character is pushed backwards.

With it, you can control the velocity and duration of the push-back.

You can activate this from code too:


1
2
3
4
5
6
7
8
MotionController lMC = Component.FindObjectOfType();
PushedBack lMotion = lMC.GetMotion<PushedBack>();
if (lMotion != null)
{
    lMotion.MaxAge = 2f;
    lMotion.PushVelocity = lMC.transform.forward * -5f;
    lMC.ActivateMotion(lMotion);
}

 

Custom Movement

If you wanted, you could move the character yourself. This way you’re 100% in control of the movement. Here we’d use the Actor Controller like we did before.


1
2
3
4
5
6
7
8
if (lPushTime >= 0f)
{
    MotionController lMC = Component.FindObjectOfType();
    lMC.ActorController.SetPosition(lMC.Transform.position + (lMC.Transform.forward * -10f * Time.deltaTime));

    lPushTime = lPushTime + Time.deltaTime;
    if (lPushTime > 0.4f) { lPushTime = -1f; }
}

With this approach, you can slow down the movement, speed it up, extend the time, etc.

Leave a Reply