0
\$\begingroup\$

I was wondering how to make animation and physics interact.

Specifically, I want to make it so when my character (rigidbody) plays a sword hit animation (swinging the sword in front of it), it makes the character move backward when the sword and the wall collide.

I want this to happen with physics. When the sword pushes against the wall, It pushed the player back because of the rigidbody.

I've been looking at Animation rigging in unity. Would it work to add à collider to my sword and the use Inverse kinematic to move it ? How can I achieve this?

\$\endgroup\$
0
\$\begingroup\$
  1. Add a collider to your Sword.

  2. On your Animator add a parameter > bool swordCollider, And and add your animation.

  3. Create a script with:

void OnTriggerEnter () {
  if (other.gameObject.CompareTag("Wall")) {
    animator.CrossFade("KnockBack", 0.2f);
  }
}
  1. Add your script on the Sword.

Done 👍

\$\endgroup\$
3
  • \$\begingroup\$ I'm surprised you'd manually initiate a crossfade rather than setting the bool (or trigger) parameter on the animator and letting the animator handle selecting the appropriate transition. Can you talk about why you'd recommend doing it that way? Also, don't forget to mention that the sword collider should be set to isTrigger. \$\endgroup\$
    – DMGregory
    Nov 28 '21 at 14:37
  • \$\begingroup\$ Is your script making a knockback animation when the sword hits the wall? What I meant in my question, is how to my it so when my sword hit the wall, my character goes back with physics, like you would go back if you pushed a wall. \$\endgroup\$ Nov 29 '21 at 13:21
  • \$\begingroup\$ @SamuelFyckes I think Xeno Gaming is assuming here that you are using the root motion of your animations to drive your character's physical motion. So when the character plays a knock back animation and moves back in their animated coordinate system, that also displaces the character object / controller / rigidbody to exactly match. If you are not using animations to drive your physics in this way, that would be a good detail to add to your question. There are many ways to set up a character in a Unity game, so it's hard for users to accurately guess your setup unless you document it for us. \$\endgroup\$
    – DMGregory
    Nov 29 '21 at 13:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .