Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by Lee-Tarry · Nov 18, 2019 at 02:04 PM · physicsraycastraycastinglinecast

Best way to get a thickness of a wall between 2 points

I have a 'Source' object that constantly watches my player. it basically emits damage the further away you are.

I would like to be able to reduce that depending on the object(s) in between the character and the source.

so far I've gotten this which tells me that there is something in-between us.

 public int results;
 public GameObject spawnMe;  //Source
 public GameObject spawnMe2;  //Character
 
 if (Physics.Linecast(spawnMe2.transform.position, spawnMe.transform.position))
         {
             Debug.Log(results);
         }

I have no idea what to do next or if this is the best way to do it, does anybody have any tips/ advice on best practice for this kind of thing?

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

2 Replies

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

Answer by lgarczyn · Nov 18, 2019 at 08:47 PM

If you are sure that there can only be one object between the source (A) and the target (B), simply do two raycasts from both direction, and subtract their distance to the distance between A and B.


If there can be multiple objects, it's more complicated.

  • You need to call two RaycastAll, one from both directions.

  • Once you have two arrays of every hit, you need to sort them both by collider.

  • Then you need to check that every collider present in one, is also present in the other. This might not be the case if A or B is inside a collider. You may delete any collider not present in both.

  • Once that is the case, you need to calculate the "thickness" of every collider, by subtracting the two distances from the distance from A to B.

  • You can then sum all the thicknesses of all the colliders.


Needless to say, this is quite complicated. Usually games just use a line of sight for that kind of effect, which simply needs a single raycast.


After popular demand by exactly one person, I took a stab at it, here's the result

     using UnityEngine;
     using System.Linq;

     public static class CustomCast
     {
         /// Returns the width of each colliders between two points
         /// Does NOT work with a concave MeshCollider or TerrainColliders
         /// If A or B are inside a collider, they will be ignored unless includePartialColliders is true
         /// If includePartialColliders is true, one-sided colliders such as Planes will have infinite width on one side
         /// Width will be larger than the actual distance if colliders overlap
         public static float DepthCast(
             Vector3 source,
             Vector3 destination,
             int layerMask = Physics.DefaultRaycastLayers,
             bool includePartialColliders = false,
             bool debug = false)
         {
             Vector3 direction = destination - source;
             float distance = direction.magnitude;

             if (debug)
                 Debug.DrawLine(source, destination, Color.red, 1f);

             // Casting from both direction to get both front and back faces of the collider

             RaycastHit[] sourceHits = Physics.RaycastAll(source, direction, distance, layerMask);
             RaycastHit[] destinationHits = Physics.RaycastAll(destination, -direction, distance, layerMask);

             // Sorting the result to group colliders and check that they do come in pairs

             RaycastHit[] sortedHits = sourceHits.Concat(destinationHits).OrderBy(x => x.collider.GetInstanceID()).ToArray();

             float totalWidth = 0f;

             for(int i = 0; i < sortedHits.Length;)
             {
                 RaycastHit currentHit = sortedHits[i];
                 if (i < sortedHits.Length - 1)
                 {
                     RaycastHit nextHit = sortedHits[i + 1];

                     if (nextHit.collider == currentHit.collider)
                     {
                         float width = distance - (nextHit.distance + currentHit.distance);

                         if (width < 0f)
                             Debug.LogWarning("Incorrect width for " + nextHit.collider.name + " of: " + width);
                         else if (debug)
                             Debug.Log("width of object " + nextHit.collider.name + " = " + width);

                         totalWidth += width;

                         if (i < sortedHits.Length - 2 && sortedHits[i + 2].collider == currentHit.collider)
                         {
                             Debug.LogWarning("Collider present more than twice for " + nextHit.collider.name);
                         }

                         i += 2;
                         continue;
                     }
                 }
                 if (includePartialColliders)
                 {
                     float width = distance - currentHit.distance;
                     totalWidth += width;
                 }
                 i++;
             }

             return totalWidth;
         }
     }


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 Lee-Tarry · Dec 03, 2019 at 01:27 PM 0
Share

This is awesome thanks,

I'm just going through making this fit my source/target as the target will be the moving player.

I will then attempt to extract each 'wall' separately because as a final product I would like to have multiple walls (of different material maybe I will use hit.transform.tag for this and just name them differently) and to be able to calculate the damage through these walls based on their thickness and density.

I will let you know if I get anywhere with it, thanks again for all the help @ceandros

avatar image lgarczyn Lee-Tarry · Dec 03, 2019 at 04:35 PM 0
Share

To have different wall densities, you simply need to add these lines after both width calculations

 var wall = currentHit.collider.GetComponentnt<YourWallScript>()
 if (wall)
     width *= wall.density



avatar image
0

Answer by Lee-Tarry · Nov 21, 2019 at 10:39 PM

As @ceandros mentioned this is my script for finding the thickness of an object between a target and a source.

this only works with one object so if theres any math geniuses out there who know how to make this work for multiple obstructions let me know!

Thanks again @ceandros

 public class Rad : MonoBehaviour
 {
         public GameObject targetR;
         public GameObject source;

     public float D;
     public float D1;
     public float D2;
     public float D3;
     public float MaxDist = 100f;
     Ray ray;
     RaycastHit hit;
 
     void FixedUpdate()
     {
 
         RaycastHit hit;
             Vector3 playerDirR = (targetR.transform.position- source.transform.position);
             Vector3 sourceDir = (source.transform.position - targetR.transform.position);
         D = Vector3.Distance(targetR.transform.position, source.transform.position);
         D3 = D - (D1 + D2);
         RadCalc = 0;
      
         // Bit shift the index of the layer (8) to get a bit mask
         int layerMask = 1 << 8;
 
         // This would cast rays only against colliders in layer 8.
         // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
         layerMask = ~layerMask;
 
        //SOURCE - TARGET
         if (Physics.Raycast(source.transform.position, playerDirR, out hit, MaxDist, layerMask))
         {
             D1 = hit.distance;
             Debug.DrawRay(source.transform.position, playerDirR, Color.red);
             Debug.Log("1 Did Hit " + D1);
         }
         else
         {
             Debug.DrawRay(source.transform.position, playerDirR, Color.green);
         }
         //TARGET - SOURCE
         if (Physics.Raycast(targetR.transform.position, sourceDir, out hit, MaxDist, layerMask))
         {
             D2 = hit.distance;
             Debug.DrawRay(targetR.transform.position, sourceDir, Color.blue);
             Debug.Log("2 Did Hit " + D2);
         }
         else
         {
          Debug.DrawRay(targetR.transform.position, sourceDir, Color.yellow);

         }
     }
 }
 
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 lgarczyn · Nov 27, 2019 at 03:51 AM 0
Share

I added my solution, but good on you for trying, your solution is good enough for most usage.

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

300 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 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 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 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

Strange physics raycasting behaviour 0 Answers

Linecast ignores obstacles. 0 Answers

Adding MaxDistance to RayCast Stops Raycast From working 0 Answers

How to reflect raycast rays of objects 2 Answers

Find all locations where a ray/ raycast insects an object 0 Answers


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