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,
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;
}
}
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
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
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);
}
}
}
I added my solution, but good on you for trying, your solution is good enough for most usage.
Your answer
Follow this Question
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