Using Linecast with collider bounds
I'm trying to to detect when two 3D objects overlap each other 'visually' without actually touching. For example, if I have two spheres in a scene that are 1 meter apart and I rotate my camera around the objects so that one sphere is blocked by the sphere in front of it, I want to detect when this occurs.
After many searches in this forum, I patched together the solution below. However, it only partially works: it only detects overlap when the center of the occluded object is reached (i.e., "bounds.center"). I read that there is a way to use the bounds in this scenario so that it samples multiple rays from around the object. This way, overlap is detected at the objects visual boundary and not just it's center. Unfortunately, my coding abilities are meager and I can't figure out how to do it.
Any help would be greatly appreciated.
public class OverlapCheck : MonoBehaviour {
public Transform myCamera;
private Color mouseOverColor = Color.green;
private Color originalColor;
private Vector3 objectExtents;
// Use this for initialization
void Start () {
originalColor = gameObject.GetComponent<Renderer> ().material.color;
objectExtents = gameObject.GetComponent<Renderer>().bounds.center;
}
// Update is called once per frame
void Update () {
if (Physics.Linecast (objectExtents, myCamera.position)) {
gameObject.GetComponent<Renderer> ().material.color = Color.yellow;
} else {
gameObject.GetComponent<Renderer> ().material.color = originalColor;
}
}
}