Raycast in melee

Hey guys (and girls?). I wanted to make a sword fight using Raycast (View from the 3rd person). Here’s the code:

public void sendDmg()
    {
        RaycastHit hit;

        if (Physics.Raycast(DmgRay.position, DmgRayTo.position - DmgRay.position, out hit, 4f))
        {
            if (hit.transform.tag == "Enemy")
            {
                Enemy = hit.collider.gameObject;
                Instantiate(DeadEnemy, Enemy.transform.position, Enemy.transform.rotation);
                Destroy(Enemy);
            }
        }
    }

P.s:
DmgRay - Point at the base of the sword;
DmgRayTo - Point at the end of the sword;

Like all works, but not quite right. Ray does not always gets to the target.
(Video: - YouTube)

Can tell me how to do it better? )

(Sorry for my bad English. Translated by Google =))

You already asked this question on the forum and I answered it there as well.
http://forum.unity3d.com/threads/raycast-melee-fight.330380/#post-2141197

I have recently implemented a melee system in my game. Originally I used this raycast solution but found that it wasn’t completely satisfactory.

From there I switched to a box collider on the weapon and using OnTriggerEnter to detect the collisions.

This solution works good.

However, I recently found a third method which I’m currently using.

Which, is to use Physics.OverlapSphere to detect the collisions.

    Collider[] hits = Physics.OverlapSphere(collisionPoint.position, .5f);

    foreach (Collider hit in hits)
    {
        if (hit.transform.root != transform) // can use this or layers to stop from hitting yourself
        {
            Debug.Log(hit.name);
        }
    }

I use Physics.Spherecast to continuously check for all surrounding actors, and put those that are in a defined front arc into an array, and if a melee attack is to be made, I pull the first element from the array.

private GameObject[] CheckEnemies(float maxRange, float maxAngle) {

		List<GameObject> enemies = new List<GameObject>();
		Collider[] colliders = Physics.OverlapSphere(transform.position + transform.up * 0.5f, maxRange, actorMask) ;
		foreach(Collider col in colliders) {

			Vector3 targetDir = new Vector3(col.transform.position.x, transform.position.y, col.transform.position.z) - transform.position;
			Vector3 sourceVector = transform.forward;
			sourceVector.y = 0f;
			float angle = Vector3.Angle(sourceVector, targetDir);
			if(angle < maxAngle) {
				enemies.Add(col.gameObject);
			}
		}
		return enemies.ToArray();
	}