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))
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?
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
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(
int layerMask = Physics.DefaultRaycastLayers,
bool includePartialColliders = false,
bool debug = false)
Vector3 direction = destination - source;
float distance = direction.magnitude;
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*;*
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;
float width = distance - currentHit.distance;
totalWidth += width;
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;
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);
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);
Debug.DrawRay(targetR.transform.position, sourceDir, Color.yellow);