I get this property or indexer cameraraycaster.layerhit can’t be assigned to (it’s read only). here is the code
using UnityEngine;
public class CameraRaycaster : MonoBehaviour
{
public Layer[] layerPriorities = {
Layer.Enemy,
Layer.Walkable
};
[SerializeField] 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; }
}
public delegate void OnLayerChange(); // Declare delegate type
public OnLayerChange layerChangeObservers; // Instantiate observer set
void Start()
{
viewCamera = Camera.main;
layerChangeObservers (); // Call delegates
}
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;
if (layerHit != layer) { //if layer changed
layerHit = layer;
layerChangeObservers(); //call delegates
}
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;
}
}