Sword damage with Collider or Raycast ?

Hello I have a question about sword and dealing damage.

I am trying to create 3rd person RPG styled game with sword combat.
(Player character has 3 attacking animations and the sword is attached to a hand as a children)

My problem is i have a script where enemy gets damage if its collides with sword
(Sword has a box collider that is set to triger) but when it collides with the sword it doesn’t take damage ( the sword is moving when animation is played) but when i create a sphere collider that is set to trigger on the player gameobject the enemy takes a damage.

I want to make my game with hitboxes so that the enemy/player takes damage only when it hits the collider.
I have tried looking for answers on youtube but i have found only videos where they are using raycast.

Is there a way how to make this work without using too much code ? or should i use raycast ?

Normally if you want to move a rigidbody, you want to call .MovePosition() on it with its new location and .MoveRotation() with its new rotation. If you just assign new values to the transform, from the perspective of the physics engine I believe that is like a “warp” to the new location, and collisions might not trigger.

I’m not sure what happens if you drive a rigidbody-equipped collider with a Unity animation. If it detects and drives the rigidbody correctly, you should get the collisions, but if it just drives the transforms (which I’m guessing it does), then you won’t necessarily get collisions.

What might be necessary is to put another gameobject in the same location and rotation as the sword. This new object would have the collider and rigidbody. Then on LateUpdate() have a script copy the position and rotation, and explicitly call the .MovePosition() and .MoveRotation() of the rigidbody to make sure all collisions get hit.

It might be tricky enough to post this to the physics board. They might have a more definitive answer for you.

1 Like

Thank you got it. i have managed to fix this I forgot to attach rigidbody to the enemy.