- Home /
I'm having troubles using a Capsule Cast
I've started to add different guns to my game, most recently a shot gun, up until recently I've been using raycasts to detect if I'm shooting something, which is fine for most guns, but I wanted the Shotgun to have more of a spread, so you don't have to be as accurate, so I looked up how I could somehow widen a Raycast, I've found out about the Capsule Cast, which can do exactly what I wanted.
I'm having problems using it though, with some guns, it works great, but with others, the accuracy's way off, way above or below the crosshair.
Here's my code:
shooting = true;
RaycastHit hit;
Vector3 directionRay = gunMuzzle.TransformDirection(Vector3.forward);
//Vector3 capsulePoint2 = gunMuzzle.position + directionRay;
Debug.DrawRay (gunMuzzle.position, directionRay * range, Color.red);
if (bulletsLeft > 0 || isMeleeWeapon)
{
if (Physics.SphereCast(gunMuzzle.position, spread, directionRay, out hit, range))
{
if (hit.rigidbody)
{
if (hitParticle)
{
hitParticle.transform.position = hit.point;
hitParticle.transform.localRotation = Quaternion.FromToRotation(Vector3.forward,
hit.normal);
hitParticle.Emit();
}
hit.rigidbody.AddForceAtPosition(directionRay * force, hit.point);
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
if (isMeleeWeapon)
{
audio.PlayOneShot(reloadSound);
}
}
else if (hit.collider.gameObject.CompareTag("Kruncanite"))
{
if (!bloodEffect)
{
bloodEffect.transform.position = hit.point;
bloodEffect.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, hit.normal);
bloodEffect.Emit();
}
}
}
What am I doing wrong?
Without seeing how your weapons are set up I can't really say for sure, but you are basing the direction off of the gun object itself which if not perfectly oriented to the camera could definitely cause this issue. Are you sure that's not the case?
I'm pretty sure, I have bullets co$$anonymous$$g out of the same empty/transform, and they come out and end up at the centre/crosshair no problem. I've tried having the capsule casts come out of the camera, some guns worked, some didn't.
I've discovered what was causing it: The radius part, for some reason, I've noticed the higher the radius is, the higher up the collider is, what's causing that?
Do you know what could be causing that? How could I fix it?
Answer by Golan2781 · Feb 24, 2013 at 12:58 PM
The way you are using SpherCast/CapsuleCast - I don't think it does what you think it does.
Both Sphere- and CapsuleCast will return only ONE hit, namely the first object they hit on their path. So using them for a multi-projectile weapon is NOT going to working.
There are two obvious alternatives:
Keep using RayCast, but cast many of them. Basically every pellet of a shotgun blast would be represented by one RayCast. Use the Random library to have each pellet follow a different path, thereby creating spread; the best implementation of this depends on the kind of game you are making (2D, 3D, arcade, realistic, ...).
Use the SphereCastAll, which will return ALL possible hits. This solution will require the most work from you, as you will have to set up rules on how to handle different cases of objects and hit characteristics. For example, an object with many colliders would register more hits; similarly, you likely want a bigger object to receive more damage. It will also return hits that would have been blocked by other objects.
The Raycast solution works great, thanks!
I'm a little concerned with performace, how costly is a raycast? How many can I use?
Can't give you an exact number, but the entire physics library (this includes raycasts etc.) is very efficient. Unless you hit ~1000 simultaneous rays, I wouldn't bother worrying.
The main strain is going to be what you do with the information gained for the casts.
Answer by nventimiglia · Feb 24, 2013 at 01:40 AM
I use a sphere cast for my shotguns.
http://docs.unity3d.com/Documentation/ScriptReference/Physics.SphereCast.html
Its a sphere that starts at origin and then is cast forward like a thick ray.
I'm afraid that's made things even worse, it's random whether or not it actually registers a shot, even if I shoot the exact same spot.
I've update the code in the question, so you can have a better look.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Problem with getting a value from a enum 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Trouble Understanding Vector3.Angle() Result 2 Answers
RigidBody Script Conflict 1 Answer