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 chronicfail · Jul 15, 2013 at 01:46 PM · raycastbulletlinecastraycastallpenetration

Penetration weapon needs to find all objects it passes through

I am making a weapon where the bullet can sometimes pass through certain walls and hit items on the other side, and so far I have this code:

 #pragma strict
 
 private var me : Transform;
 private var rigid : Rigidbody;
 private var oldPos : Vector3;
 
 function Start () {
 me=transform;
 rigid=rigidbody;
 oldPos=me.position;
 }
 
 function Update() {
 var entry : RaycastHit;
 if(Physics.Linecast(oldPos,me.position,entry)){
 var exit : RaycastHit;
 if(Physics.Linecast(me.position,oldPos,exit)){
 var thickness=CollisionThickness(entry,exit);
 print(thickness);
 }
 }
 oldPos=me.position;
 }
 
 function CollisionThickness (entry : RaycastHit, exit : RaycastHit) {
 var colthickness=Vector3.Distance(entry.point,exit.point);
 return colthickness;
 } 

which runs every frame on my colliderless rigidbody bullet to check whether it passed through an object that frame. It also finds how thick the object was it passed through, which I will use to calculate whether or no it should penetrate or not. But it often passes through multiple colliders in one go, and only detects the first and last entry/ exit points. This means it gives misleading thickness data, and will fail to place bulletholes on some surfaces when I have added that code in. What I want is for it to detect all entry/ exit points it goes through each frame, and get data from all of them. I tried Physics.RaycastAll but couldn't get it to work properly. Please could someone help with this as I have not been able to find a solution anywhere else?

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

Answer by Brian@Artific · Jul 15, 2013 at 03:35 PM

I think you were on the right track using Physics.RaycastAll - the trouble likely came from the fact that RaycastAll does not necessarily return a sorted array of hits. You can sort the hits by adding your own Comparison function:

     static int CompareRaycastDistances(RaycastHit x, RaycastHit y)
     {
         if (x.distance > y.distance)
             return 1;
         if (x.distance < y.distance)
             return -1;
         return 0;
     }

... then pass this to your sort method:

         RaycastHit[] hits = Physics.RaycastAll(me.position, me.forward);
         Array.Sort(hits, CompareRaycastDistances);

... so that you have a list of hits in order from the first collider to the last. For the exits, just run the same cast in the other direction and sort.

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 chronicfail · Jul 17, 2013 at 03:54 PM 0
Share

thank you, I did not even realise there was a RaycastHit.distance, and have been finding it the long way with Vector3.Distance up to now. That will work for raycast weapons with no travel time/ bullet drop, but I don't think it is possible to limit the range of a RaycastAll, so when I need to check all hits between point A and point B in each segment of a ballistic trajectory, it will ins$$anonymous$$d find all hits from point A through point B for infinite distance.

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

16 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

Related Questions

Simulating Bullet Gravity using Raycasting 1 Answer

EteeskiTutorials' bullets (raycast bullets with gravity) 1 Answer

Ricochet Problem of projectile bullets 0 Answers

Multiple raycasts in same fixedupdate 3 Answers

Simple Raycast Penetration 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