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 TheNoobieWaffle · Jun 27, 2015 at 02:04 PM · raycastcollider

How to use a Raycast to see the distance traveled within a collider.

I am doing bullet penetration and was wondering how I could achieve this. I just want to raycast to the point that I hit a wall then do another raycast from that end point to see the distance the bullet travels inside the wall. Can anyone enlighten me? :D Thanks -- Michael

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

2 Replies

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

Answer by zach-r-d · Jun 27, 2015 at 02:17 PM

Check out Physics.RaycastAll. Instead of stopping the raycast at the first thing hit, it continues to go through everything hit, in the whole scene. Using this, it is possible to sort the list of hits by distance, go through the sorted list in order, and decide at what point the bullet has run out of energy.

Note that this will only give hits for where the bullet enters, not exits. To get a list of exits, it'll take another RaycastAll in the negative direction. After that, though, it should be possible to match up entrances to exits, and get the length of the vector from an entrance to an exit to figure out how far it traveled inside a particular collider.

Comment
Add comment · Show 6 · 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 adriandevera · Jun 27, 2015 at 02:27 PM 0
Share

Oh youve beat me to the raycastall, but you forgot to mention you need two colliders others the second wont trigger a RaycastHit deter$$anonymous$$e the lengths

RaycastHighWithinWall - RaycastHitImpactOrigin = distance traveled within wall

avatar image zach-r-d · Jun 27, 2015 at 02:31 PM 0
Share

Two colliders are not necessary. As I mention in the second paragraph, perfor$$anonymous$$g a second RaycastAll in the negative direction of the first will find exit points without having to make separate colliders.

avatar image adriandevera · Jun 27, 2015 at 03:24 PM 0
Share

I agree but I think a box collider is more efficient than a second unnecessary raycast, correct?

avatar image zach-r-d · Jun 27, 2015 at 03:48 PM 0
Share

I'm not sure I understand what you're saying; perfor$$anonymous$$g the second negative raycast works with any kind of collider. If you're suggesting having two colliders per wall, remember that perfor$$anonymous$$g one raycast on double the number of objects will be about the same as perfor$$anonymous$$g two raycasts on the original number of objects in terms of efficiency.

avatar image TheNoobieWaffle · Jun 28, 2015 at 11:51 AM 0
Share

Perfect, thanks! Works like a charm I actually got this idea as well in the long time unity takes to accept a question. Although I didnt use raycast all because the game has bullet drop as well so from tha point it penetrates some of the stats of the bullet drop. It goes through its target then it will raycast again instantly to see if it tavels through another target.

Show more comments
avatar image
1

Answer by adriandevera · Jun 27, 2015 at 02:27 PM

Well you could use RaycastHit then take the point.origin to see the location of entry. The thing is that the ray will only go until it hits one collider.

To go through multiple I believe you have to use RaycastAll: http://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html It will cast a ray and return all the hits. You can store the first hit as "impactorigin" then the second hit as "impact length"

The problem with this is that for the second to be trigger youd need a collider within the wall which would be predefined, meaning itd be hard to do this dynamically without any further coding and messing with the inside collider?

If youre using different calibers of a bullet, you can probably do this: Make wall with box collider. Then make an Empty Gameobject with another box collider (box because its less intensive to compute unlike mesh? Or is it the other way around, i forget). Then parent that Object to the Wall itself. Ensure the empty gameobject is within the wall.

Now upon a script you can do like a bool on the wall OnCollission then GetComponentFromChildren() then change position of the inside collider where closer to the wall surface is for smaller calibers (hence low penetrations) and the opposite for larger calibers.

And thus you can do the following:

RaycastHitDist - RaycastHitOrigin = the bullet travel distance

Im about to leave right now so I cant give you the exact code or test it out myself anytime soon (out for the weekend)

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 TheNoobieWaffle · Jun 28, 2015 at 11:53 AM 0
Share

Thanks, as well.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raycast to align character on MeshCollider normals 1 Answer

Raycast missing moving Collider 1 Answer

Per-polygon ray tracing? 1 Answer

Raycast Collider Tag not returning correct result 1 Answer

Rays: why is not working? 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