CapsuleCastAll not returning hits

Hey guys, I’m currently working on trying to implement some abilities for characters, the ability I’m working on at the moment is meant to teleport the player forward and deal damage to all the enemies lying between where the player started and where they finished, with a certain with, so I’m using a CapsuleCastAll with a layer mask specifically for enemies and then merely calling a deal damage function for all the enemies returned, only for some reason it doesn’t seem to be hitting enemies that are CLEARLY well within the cast, and sometimes it returns none at all. I tried making the radius huge and it changes nothing.

				p1 = myTransform.position + controller.center + Vector3.up * -controller.height * 0.5F;
				p2 = p1 + Vector3.up * controller.height;
				layerMask = 1 << 12;
				abilityTars = Physics.CapsuleCastAll(p1, p2, controller.radius * 5f, myTransform.TransformDirection(Vector3.forward), range);
				p3 = p2 + myTransform.TransformDirection(Vector3.forward) * range;
				Debug.Log("enemies hit = " + abilityTars.Length);

				if (range < 1.5)

				{
					Debug.Log("dashing into wall");
					movement = myTransform.TransformDirection(Vector3.forward) * (range - 0.3f);
				}
				else
				{
					movement = myTransform.TransformDirection(Vector3.forward) * range;
				}

				controller.transform.position += movement;
				int enemiesHit = 0;
				foreach (RaycastHit hit in abilityTars)
				{
				    if (hit.collider.gameObject.tag == "Enemy")
				    {
					    enemiesHit++;
						GameObject Enemy = hit.transform.gameObject;
						EnemyDamage enemy = Enemy.GetComponent<EnemyDamage>();
						if (enemy == null)
							continue;
						enemy.SendMessage("ApplyDamage", 2);
						stats.WriteText(Enemy.transform.position, 2, null, Color.white);

				    }
                }

any ideas on what may be happening?

A CapsuleCast has the same limitations as a Raycast. That means if it starts inside the object, it can’t hit the object. Making the radius huge will just skip more objects which are within the initial position’s sphere.

The best way for area effects is Physics.OverlapSphere. It only tests against the colliders bounding box and not the true collider, but it’s very fast and reliable.

What you are looking for here is Rigidbody.SweepTestAll- it does almost exactly what you want here! You can use it for any kind of collider, provided that it is controlled by a rigidbody (it can be kinematic if you don’t need physics).