Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 s5ehfr9 · Nov 05, 2018 at 11:12 AM · scripting problembulletsphysics.raycast

Using raycast to determine the thickness of a collider

I'm trying to make a script that gives a bullet realistic penetration effects on impact with objects. To do this, I need to know the (effective) thickness of the object (collider, to be precise) the bullet collides with. To do this, I made two raycast operations in opposite directions along the bullet's velocity with a separation of two meters. Here's the code:

 //rb is the rigidbody of the bullet
 Ray ray = new Ray(transform.position, rb.velocity);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit, rb.velocity.magnitude ))
         {
             RaycastHit[] otherDirectionHits;
             otherDirectionHits = Physics.RaycastAll(hit.point + ray.direction * 2, -ray.direction, 2);
             float effctiveThickness = 0;
             for (int i = otherDirectionHits.Length - 1; i > 0; i--)
             {
                 if (hit.rigidbody == otherDirectionHits[i].rigidbody)
                 {
                     effctiveThickness = 2  - otherDirectionHits[i].distance;
                     break;
                 }
             }
             Debug.Log(effectiveThickness)
         }

However, whenever the bullet hits anything, the Debug.Log outputs 0.

I have tested this script with different sizes of box colliders, static mesh colliders, terrain colliders and convex mesh colliders and the output is always the same.

What have I done wrong? Is it with the raycast API? I would appreciate any kind of help. Thanks.

Comment
Add comment
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

1 Reply

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

Answer by _dns_ · Nov 05, 2018 at 03:12 PM

Hi, I would use Debug.DrawLine or .DrawRay to see what is happening. Also, the for() on line 9 should include the case where i == 0 (using "i >= 0"). I would also try to use some non-allocating versions of Physics methods, it's always a good optimization to do, once it works of course :-)

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 s5ehfr9 · Nov 05, 2018 at 03:36 PM 0
Share

Thank you. I have tested it again with the i > 0 changed to i > = 0 and it worked. I'm new to C# so I'm not sure if the for loop is pre-condition or post-condition or when the increment is executed. Guess I'll have to be more careful next time to avoid these careless mistakes.

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

95 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Raycast Only Hits an Object if the Previous Raycast hits the Object 1 Answer

How to make a bullet move toward player and keep moving past him 1 Answer

Raycasting only works when close to an object 0 Answers

Bullet Not Working 3 Answers

shooting raycast 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