Making a sword "cut" through the target after collision

I’m working on a medieval combat game (in 3d) and right now I’m a bit stuck on how to handle weapons. I have been thinking about this issue for a bit and have a few ideas, but I’m not quite confident enough that I understand physics in unity well enough to estimate if they work.
The weapons are moved through animations, as such they should probably be kinematic rigid bodies. They will have their own collider meshes. The enemies are also animated actors, with multiple kinematic rigid bodies with mesh colliders for collision detection.

My idea is that when a weapon hits an enemy, I will be able to calculate the damage through the velocities of the weapon and target collider at the point of collision, as well as their masses. For the sake of simplicity, I’m thinking about blunt weapons (clubs, maces) that don’t cut in first. So my first question is, is it feasible to have those two kinematic rigid bodies be non kinematic for the collision, while the rest of the enemy body (and the hands, arms etc. of the attacker) are being moved through IK?
All I need to calculate the damage is the velocities of weapon and target rigid body and their masses, as well as knowing which rigid body is hit.

My second question is about the same scenario, except with sharp weapons such as swords. In that case, collisions don’t simply “bounce off”, but would in most cases cut into the target. This makes using unity’s collision physics for that impossible, as the weapon would be “bounced out” of the target, right?
My idea here was to apply an acceleration (because the rigid bodies would need to be kinematic) in the inverse direction of the attack. So basically, slow the weapon down once it starts cutting. Additionally, I’d also accelerate it into the direction of the targets movement. For this scenario, I have multiple questions…

1. Can I apply an acceleration to a certain point of the rigid bodies, so the physics engine would take care of possibly rotating the rigid body?
2. Is there a way to calculate the volume of the collision? (i.e. the volume of the part of the sword that is in the target?)

And at last… Do you have better ideas about how to handle this?

Hi Christoph, sounds like an interesting project and I’ll help as much as I can.

For the first scenario, as long as the weapons and the character are rigidbodies with colliders (and not set to trigger or kinematic), the physics engine will handle the impact, pushing the character and bouncing the weapon back. You’ll have to set an appropriate mass for the character and the weapons, which will affect who’s velocity changes more from the impact, and the bounciness of the physics materials, to control how much of the kinetic energy (speed) gets lost during the impact. To calculate the damage, simply ad an `OnCollisionEnter()` method to the script of the character, and it’s parameter will be the weapon, so you can see both speeds:

``````public void OnCollisionEnter(Collision other) {
Vector3 otherVelocity = other.rigidbody.velocity;
Vector3 myVelocity = rigidbody.velocity;
// do some damage here
}
``````

About the sword, it sounds like you’ll need the sharp weapons to be a “trigger” collider (it’s a checkbox in the collider component in the inspector). This means that a collision event will be initiated, but the collision will not be resolved by the physics engine - hence velocities will not be affected, no force will be applied by the impact.

Then you will need to handle everything in your trigger events `OnTriggerEnter()` and `OnTriggerExit()`. You can decrease the sword speed in `OnTriggerenter` a little, and increase it upon exit. Here are scripts you can use (again, attached to the character not the weapon):

``````public void OnTriggerEnter(Collider other) {
other.rigidbody.velocity *= 0.5f; // slow the sword by half
// do some damage here
}

public void OnTriggerExit(Collider other) {
other.rigidbody.velocity *= 2f; // return the velocity
}
``````

Okay, thanks! Well, in the thread it says it has to be primitive colliders, but I guess I could just as well use mesh colliders and set them as “convex” so they can collide with other collision meshes, right? So basically the only trick mentioned in that thread is setting up the constraints.
I guess the only thing I’ll need to test now is if isKinematic can be easily enabled/disabled in the game, but the documentation implies that it can be. In that case, I’ll be able to handle all of that via rigid bodies. Nice.

Yeah, good point about using the difference of the kinetic energy. I guess I was a bit too fixed on implementing what I’ve been tinkering with on paper So I’ll just go ahead and try this out. Since it’s something relatively complex for me as a starter, it might take a good while, but I’ll tell you how it turned out once I’m done. And thank you for the help!