Sword logic script not working

Hi, everyone. I have a sword logic script running on my players sword, and it should be adding force. There are no components out of the ordinary in my game save two scripts: my inventory and my item script. Both can be viewed here. The problem is sometimes it works fine, sometimes there is a delay and then it adds force, and sometimes it doesn't respond. All help is appreciated. Here is the sword logic script:

var swordPoints : Transform[];
var swingPower : int;
var showGizmosInEditor : boolean = false;

function Update () 
{
    var hit : RaycastHit;
    for(i = 0; i < swordPoints.length; i++)
    {
        if(Physics.Linecast(transform.position, swordPoints*.position, hit))*
 *{*
 _Hit(swordPoints*, hit);*_
 _*}*_
 _*}*_
_*}*_
_*function Hit(i : Transform, hit : RaycastHit)*_
_*{*_
 _*if(hit.collider.rigidbody)*_
 _*{*_
 _*hit.collider.rigidbody.AddExplosionForce(swingPower, i.transform.position, 1.0, 3.0);*_
 _*}*_
 _*if(hit.collider.tag == "Enemy")*_
 _*{*_
 _*hit.collider.gameObject.SendMessageUpwards("ApplyDamage", swingPower, SendMessageOptions.DontRequireReceiver);*_
 _*}*_
_*}*_
_*function OnDrawGizmosSelected()*_
_*{*_
 _*Gizmos.color = Color.blue;*_
 _*if(showGizmosInEditor)*_
 _*{*_
 _*if(swordPoints.length != 0)*_
 _*{*_
 _*for(i = 0; i < swordPoints.length; i++)*_
 _*{*_
 <em>_if(swordPoints *!= null)*_</em>
 <em>_*{*_</em>
 <em><em>_Gizmos.DrawLine(transform.position, swordPoints*.position);*_</em></em>
 <em><em>_*}*_</em></em>
 <em><em>_*}*_</em></em>
 <em><em>_*}*_</em></em>
 <em><em>_*}*_</em></em>
<em><em>_*}*_</em></em>
<em><em>_*```*_</em></em>

your script is completely correct and has this logic in it.

there are a set of transforms (points) which you cast a line from the pivot of your sword toward them and then if hit anything, you check if the hitpoint has a rigidbody you apply force to it. then if it also has the enemy you also send a message to it.

there are two things to consider.

  1. objects should not be in "ignore raycast" layer for raycasting to work with them.
  2. the transform points must be some points that move with your sword when you move the character (i.e children of your sword or object), otherwise the raycasting will be done toward incorrect points.

the second problem is easily visually debugable because of your gizmo part so you don't have this problem so check if your layers of objects is correct and if they have a rigidbody and their irigidbody is not kinematic. if it's correct then play with the force value, maybe it's too low and check if your objects have ApplyDamage and if yes then check if it's called or not.

place print statements inside your ifs to see if they are called or not (above addforce and sendmessage).

if not solved yet, then comment here and i'll provide more help!

I'd consider instead of using AddExplosion to just activate hit animations on the enemies. AddExplosion should be used on a coroutine and expand from the sword to the outside, not called when objects are hit by linecast. You're making things explode from inside themselves.

I may be wrong but I think it's because you're calling Hit inside Update(). As far as I am aware all physics based stuff needs to be called in FixedUpdate().