Sure thing. Thanks for the quick response.
Here are the scripts:
public enum Layer
{
Walkable = 9,
Enemy = 10,
RaycastEndStop = -1
}
public class CameraRaycaster : MonoBehaviour
{
public Layer[] layerPriorities = {
Layer.Walkable,
Layer.Enemy,
};
float distanceToBackground = 100f;
Camera viewCamera;
RaycastHit m_hit;
public RaycastHit hit
{
get { return m_hit; }
}
Layer m_layerHit;
public Layer layerHit
{
get { return m_layerHit; }
}
void Start() // TODO Awake?
{
viewCamera = Camera.main;
}
void Update()
{
// Look for and return priority layer hit
foreach (Layer layer in layerPriorities)
{
var hit = RaycastForLayer(layer);
if (hit.HasValue)
{
m_hit = hit.Value;
m_layerHit = layer;
return;
}
}
// Otherwise return background hit
m_hit.distance = distanceToBackground;
m_layerHit = Layer.RaycastEndStop;
}
RaycastHit? RaycastForLayer(Layer layer)
{
int layerMask = 1 << (int)layer; // See Unity docs for mask formation
Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit; // used as an out parameter
bool hasHit = Physics.Raycast(ray, out hit, distanceToBackground, layerMask);
if (hasHit)
{
return hit;
}
return null;
}
}
public class Cursor : MonoBehaviour
{
CameraRaycaster cameraRayCaster;
private void Start()
{
cameraRayCaster = GetComponent<CameraRaycaster>();
}
private void Update()
{
print(cameraRayCaster.layerHit);
}
}