Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cobradabest · Feb 23, 2013 at 09:31 PM · c#physicsprogramming

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?

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image theundergod · Feb 23, 2013 at 11:16 PM 0
Share

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?

avatar image Cobradabest · Feb 23, 2013 at 11:51 PM 0
Share

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.

avatar image Cobradabest · Feb 23, 2013 at 11:56 PM 0
Share

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?

avatar image theundergod · Feb 24, 2013 at 12:01 AM 0
Share

Oh okay awesome. Thanks for sharing :)

avatar image Cobradabest · Feb 24, 2013 at 12:08 AM 0
Share

Do you know what could be causing that? How could I fix it?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cobradabest · Feb 24, 2013 at 05:06 PM 0
Share

The Raycast solution works great, thanks!

I'm a little concerned with performace, how costly is a raycast? How many can I use?

avatar image Golan2781 · Feb 24, 2013 at 05:17 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cobradabest · Feb 24, 2013 at 12:03 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges