Hey All,
I can’t for the life of me figure out why my recast is ignoring the layer NotWalkable but is identifying Enemy and Walkable perfectly fine.
Here is my utility code:
public enum Layer
{
Walkable = 9,
Enemy = 10,
NotWalkable = 11,
RaycastEndStop = -1
}
All of these exist in the same order and values inside my Layer list in inspector (except for RaycastEndStop of course).
Here is my Raycaster code:
using UnityEngine;
public class CameraRaycaster : MonoBehaviour
{
public Layer[] layerPriorities = {
Layer.NotWalkable,
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; }
}
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 = new Ray(this.transform.position, -this.transform.forward*-1); //Casts ray in forward direction from camera
RaycastHit hit; // used as an out parameter
bool hasHit = Physics.Raycast(ray, out hit, distanceToBackground, layerMask);
if (hasHit)
{
return hit;
}
return null;
}
}
Btw my ray transform is different because I am adapting this to fps so I am shooting the ray forward from my camera and changing the crosshair based on what I’m looking at. It picks up Enemy and Walkable with zero issues but will go right through things like walls. My walls are the exact same prefab as any of the floor boxes I can walk on that are tagged as walkable (but are tagged NotWalkable). I have double checked the tags multiple times and they are all tagged corrected (both parent and children).
Help
Luke