RPG Course - Raycast not hitting NotWalkable layer

rpg

#1

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 :frowning:

Luke


#2

Hi Luke,

How far though the course are you at the moment?
The reason i ask is there is an issue with the raycaster near the start of the course and it doesnt return what it hits in the right order so it may be just pure luck that its only that one you are noticing.

If you havent hit the event based raycasting lectures it may be that you havent come acrross the fix yet.
We discovered the fix when trying to run over bridges the raycast was going though the bridge but returning the terrain underneath sometimes so it turned to run under rather than over it.

Hopefully this explains the issue and that there is a solution later on :slight_smile:


automatically bumped #3

closed #4

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.