Unity RayCasting help please!

Hi, I am having a weird issue with ray casting as it seems to not be picking up any collider which is in the same layer. I do not know how to fix this.

This is a big plot of code:

public class WallData : MonoBehaviour
{
    Vector2 origin, size = new Vector2();
    float distance = 1f;
    int layerNum = 14;
    bool left, up, right, down = false;
    SimpleBlockPlacer sbp;
    List<Sprite> wallSprite = new List<Sprite>();

    private void Start()
    {
        sbp = FindObjectOfType<SimpleBlockPlacer>();
        StartCoroutine(ListCopy());
        GetComponent<SpriteRenderer>().sortingOrder = 2;
        origin = new Vector2(transform.position.x, transform.position.y);
        size = new Vector2(GetComponent<BoxCollider2D>().size.x, GetComponent<BoxCollider2D>().size.y);
    }

    IEnumerator ListCopy()
    {
        foreach (Sprite wall in sbp.sprites)
        {
            wallSprite.Add(wall);
        }
        yield return new WaitForEndOfFrame();
    }

    private void Update()
    {
        CastRay();
    }

    void CastRay()
    {
        RaycastHit2D lefty = Physics2D.Raycast(origin, Vector2.left, distance, 1 << LayerMask.NameToLayer("Wall"));
        Debug.DrawRay(origin, Vector2.left * distance, Color.black);
        if (lefty) { left = true; }
        else { left = false; }

        RaycastHit2D upy = Physics2D.Raycast(origin, Vector2.up, distance, 1 << LayerMask.NameToLayer("Wall"));
        if (upy) { up = true; }
        else { up = false; }

        RaycastHit2D righty = Physics2D.Raycast(origin, Vector2.right, distance, 1 << LayerMask.NameToLayer("Wall"));
        if (righty) { right = true; }
        else { right = false; }

        RaycastHit2D downy = Physics2D.Raycast(origin, Vector2.down, distance, 1 << LayerMask.NameToLayer("Wall"));
        if (downy) { down = true; }
        else { down = false; }
            SpriteChange();
    }

    void SpriteChange()
    {
        //A bit buggy here:
        if (left == true)
        {
            if (up == true)
            {
                if (right == true)
                {
                    if (down == true)
                    {
                        GetComponent<SpriteRenderer>().sprite = wallSprite[7];
                    }
                    else { GetComponent<SpriteRenderer>().sprite = wallSprite[3]; }
                }
                else if (down == true)
                {
                    GetComponent<SpriteRenderer>().sprite = wallSprite[6];
                }
                else { GetComponent<SpriteRenderer>().sprite = wallSprite[8]; }
            }
            else if (down == true)
            {
                GetComponent<SpriteRenderer>().sprite = wallSprite[11];
            }
            else if (right == true)
            {
                if (down == true)
                {
                    GetComponent<SpriteRenderer>().sprite = wallSprite[5];
                }
                else { GetComponent<SpriteRenderer>().sprite = wallSprite[1]; }

            }
            else { GetComponent<SpriteRenderer>().sprite = wallSprite[12]; }
        }
        else if (up == true)
        {
            if (down == true)
            {
                GetComponent<SpriteRenderer>().sprite = wallSprite[2];
            }
            else { GetComponent<SpriteRenderer>().sprite = wallSprite[13]; }
        }
        else if (right == true)
        {
            if (up == true)
            {
                if (down == true)
                {
                    GetComponent<SpriteRenderer>().sprite = wallSprite[4];
                }
                else { GetComponent<SpriteRenderer>().sprite = wallSprite[9]; }
            }
            else if (down == true)
            {
                GetComponent<SpriteRenderer>().sprite = wallSprite[10];
            }
            else { GetComponent<SpriteRenderer>().sprite = wallSprite[14]; }
        }
        else if (down == true)
        {
            GetComponent<SpriteRenderer>().sprite = wallSprite[15];
        }
        else { GetComponent<SpriteRenderer>().sprite = wallSprite[0]; }
    }
}

Never mind i have figured it out! The issue was that the ray was detecting the collider in which it was spawning so… i had to turn off a option in the unity settings it is the “Queries Start In Colliders” . Which can be found in Edit>ProjectSettings>Physics2D.

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

Privacy & Terms