- Home /
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?
I have a similar issue: http://answers.unity3d.com/questions/401554/capsulecastall-fails-after-20-calls.html#comment-401565
I don't know what's going on here, but I can demonstrate that the first 20 CapsuleCastAll() calls seem to work right, and all calls after that point return nothing. I don't have any idea why though. It's like there's some pool of stored collisions I have to clear out or something.
Answer by Bunny83 · Apr 11, 2013 at 03:23 PM
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.
Answer by syclamoth · Sep 27, 2011 at 01:12 PM
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).
I had many many complications with rigidbodies, so the enemies are actually currently using character controllers, is there a sweep for that? Or do I really need to change them to rigidbodies?
CharacterControllers are usually more trouble than they're worth, in my experience. The simple functionality which they make easier is by far countered by the sheer number of things they make more difficult!
well for me, despite manipulating the velocity of the rigidbodies directly, they were still getting affected by drag and mass, which seems retarded, and they were just jittering about a lot, and dissapearing and reappearing and flipping around, it was very infuriating, so there's no way to sweep character controllers?
If you set your rigidbody to rigidbody.is$$anonymous$$inematic = true, then you won't have to worry about all that jittering, and you can move it just with the transform.
Ok, I've decided to take your advice and swap them back to rigidbodies, but bearing in $$anonymous$$d that they're now kinematic, they obviously don't collide with eachother, the floor and walls and use gravity, what can/should I be doing to do this then? And will this be processor intensive over just using the physics engine?