At around the time we completed the cursor affordance script, I noticed that I wasn’t getting a change when mousing over or clicking on an object on the enemy layer. I triple checked all my scripts and inspector values and couldn’t find the issue, but saw that we were going to do more work on it, so plodded ahead. I’ve now completed through Lecture 23, and it still doesn’t work. Additionally, the unknown cursor has stopped working. I’m using Unity 5.5.4
The enemy
DynamicCursor.cs (cursor affordance script)
[RequireComponent(typeof(CameraRaycaster))]
public class DynamicCursor : MonoBehaviour
{
[SerializeField] Texture2D WalkCursor = null;
[SerializeField] Texture2D EnemyCursor = null;
[SerializeField] Texture2D UnknownCursor = null;
[SerializeField] Vector2 CursorHotspot = new Vector2(0,0);
private CameraRaycaster _cameraRaycaster;
void Start ()
{
_cameraRaycaster = GetComponent<CameraRaycaster>();
_cameraRaycaster.LayerChangeObservers += OnLayerChange;
}
void OnLayerChange (Layer newLayer) {
switch (newLayer)
{
case Layer.Walkable:
Cursor.SetCursor(WalkCursor, CursorHotspot, CursorMode.Auto);
break;
case Layer.RaycastEndStop:
Cursor.SetCursor(UnknownCursor, CursorHotspot, CursorMode.Auto);
break;
case Layer.Enemy:
Cursor.SetCursor(EnemyCursor, CursorHotspot, CursorMode.Auto);
break;
default:
Debug.LogWarning("Unknown layer hit by mouse cursor!");
return;
}
}
}
CameraRaycaster.cs
public class CameraRaycaster : MonoBehaviour
{
public Layer[] LayerProperties =
{
Layer.Walkable,
Layer.Enemy
};
[SerializeField] float _distanceToBackground = 100f;
private Camera _viewCamera;
private RaycastHit _raycastHit;
public RaycastHit Hit
{
get { return _raycastHit; }
}
private Layer _layerHit;
public Layer CurrentLayerHit
{
get { return _layerHit; }
}
public delegate void OnLayerChange(Layer newLayer);
public event OnLayerChange LayerChangeObservers;
void Awake () {
_viewCamera = Camera.main;
}
void Update () {
foreach (Layer layer in LayerProperties)
{
var hit = RaycastForLayer(layer);
if (hit.HasValue)
{
_raycastHit = hit.Value;
if (_layerHit != layer)
{
_layerHit = layer;
if (LayerChangeObservers != null) LayerChangeObservers(layer);
}
_layerHit = layer;
return;
}
}
_raycastHit.distance = _distanceToBackground;
_layerHit = Layer.RaycastEndStop;
}
RaycastHit? RaycastForLayer(Layer layer)
{
int layerMask = 1 << (int) layer;
Ray ray = _viewCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool HasHit = Physics.Raycast(ray, out hit, _distanceToBackground, layerMask);
if (HasHit)
{
return hit;
}
return null;
}
}
Please let me know if there’s anything else you’d like to see. I appreciate any extra eyes on this, I’ve been over it too many times to see it anymore 
EDIT: Thought I should mention, when I mouse over or click off the ground plane (eg. off the edge of the “world”), both the RaycastEndStop and default cases trigger and log tot he console. They do not trigger when mousing over or clicking on geometry that is not assigned to a layer.


