layerHit always returns as enemy

I can’t get the raycast to recognise walkable areas (I’ve triple checked my layer settings) . It always returns the enemy layer. The only way I can get it to return the walkable layer is if I change the layerMask variable in RaycastForLayer to 2 instead of 1.

Can anyone help me? I literally can’t continue with the course unless this issue is resolved. I’ve tried the discord and haven’t had a response.

Hi James,

Could you pop up a screenshot of the layer priorities array from the Inspector for the CameraRaycaster script component (I think that was the component from memory that had this).

Also, perhaps copy/paste up your scripts so we can have a look?


See also;

Sure thing. Thanks for the quick response.
Screenshot_1

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);
    }
}

Apologies for the strange formatting. I have no idea how to post code on here.

I have no idea how to post code on here.

That’s why I gave you the link to find out how :slight_smile:

From memory, weren’t there layers in the layer priorities around the other way, e.g. enemy first, then walkable after? I could be wrong, give it a try.

Sorry, I assumed it was a signature so didn’t pay attention to it. My bad.

Switching them around doesn’t make a difference, unfortunately.

Sometimes I wish it was :wink:

Could you zip up your project files and share them so I can take a look? Where you are in the course doesn’t map up with where I was when I last looked at it, so I can’t really just compare.

Also, be aware that you change a fair bit of this functionality later on, so I wouldn’t worry too much if it isn’t quite right just yet. That being said, happy to take a look and see if there’s anything I can spot.

The forum will allow uploads of up to 10mb, I suspect your zipped project files will be larger than that so you’ll need to use a service such as Google Drive or Dropbox and then share the URL.

First time I’ve tried this. See if this link to the download.

https://drive.google.com/open?id=1wOLjgYkmgxc8UDTnnMmHkkWFR1ez6qUZ

Grabbing it now.

I’ve just compared your CameraRaycaster.cs script to the one linked from the lecture to Project Changes, the only two differences are the order of the items in the LayerPriorities array at the top of the script, and the inclusion of the enum, which isn’t present in the course version. Presumably this is in Utility.cs instead (I think that’s what it was called)

Incidentally, I think this is the lecture where you’ll find that you don’t get the Unknown cursor appear, this is corrected in the following lecture I believe when Ben spots it isn’t working :slight_smile:

Yes, it’s in Utility.cs in my project as well.

Just opening the project now… give me a little time and I’ll reply in due course :slight_smile:

No worries. Thanks again. :smiley:

Hi,

First test - change the order of the Layer Priorities as mentioned above… problem resolved.

image

I like the snow btw :slight_smile:

Oh, when you said change the order I thought you meant within the script. I switched their order at the top of the script. My bad.

Thanks for figuring it out. It was driving me nuts haha.

The script would have been overwritten with the values in the Inspector anyway, just as a headsup. :slight_smile:

You’re very welcome - enjoy the rest of the course :slight_smile:

Incidentally, if you want to give Ethan some clothes, I posted (ages ago) about the missing materials… I’ll see if I can find it… bear with me…

Here you go : Standard Assets : Let's Dress Up Ethan

Thanks for that. :smiley:

1 Like

No worries… now you can go forth and brighten your world - or at least Ethan :slight_smile:

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

Privacy & Terms