Raycast2D keeps hitting itself. So how to prevent raycast2D hitting itself.

   public void FixedUpdate()
    {
        Debug.DrawRay(transform.position, transform.TransformDirection(Vector2.down), Color.red, 0.1f);
        RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.down), 0.2f);


        if (hit)

        {
            if (hit.collider.tag == "Ground")
            {



                Destroy(this.gameObject);
            }
        }

    }

The ray is hitting the gameobject itself instead of the ground. I have seen many questions in the forum about this but no clear solution yet. I tried Mask layers but the problem remains. So how to prevent raycast2D hitting itself. The player detects only itself but not the ground

As allways thank you for your time :)

Hi @Realspawn1

Most of Unity raycast methods give you option to provide a LayerMask - I wonder what problem you had with it...

Put your Player (or whatever the source is) in some layer and then have raycast target objects in another layer. Use that layer as LayerMask for raycast.

Another way is to start raycast a small distance outside your collider.

So something like this will detect your enemy/ground/whatever is set to have a layer assigned in Inspector.

public class Raycast : MonoBehaviour
{
    [SerializeField] LayerMask layerMask = 1 << 8; // my enemy layer

    void Update()
    {
        // raycast right from center of player collider
        var ray = Physics2D.Raycast(transform.position, transform.right, 100f, layerMask);

        if (ray)
        {
            Debug.Log("hit: " + ray.collider.name);
        }
    }
}
1 Like

I will take a short break :) These things drive me crazy. I'll try to take things step by step :) So won't dive into any new codes unless i solve this :)

I tried setting IgnoreRaycast on player. That works however it does not notice the collision with the ground :)

Layermasks are almost certainly what you want. Simplify everything, make a new scene, a brand-new script, TEST, TEST, TEST, understand how the layermasks works before trying to wedge it into your game. If you don't do this then you have no way to know if you are actually getting the layermask part correct, or if there is something else going on. Also, use Debug.Log() EVERYWHERE... print the name of the collider you hit, print the name of where the script is running, etc., etc., etc.

1 Like

Kurt A fellow dutch man ? :)

Getting closer. I tested this and it works. So something in my player code is interfering :)
Post by Realspawn1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class testray : MonoBehaviour
{

    private int layerMask = 1 << 8;


    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        Debug.DrawRay(transform.position, Vector2.down,Color.red, 0.2f);

        RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, 0.2f);    
        if (hit.collider != null)
        {

            if(hit.collider.CompareTag("Ground"))
            {
                Debug.Log("hit something");
                Destroy(this.gameObject);

            }
        }
    }
}
1 Like

Awesome! Go back to your oplayer code and start sprinkling in Debug.Log() statements like you're salting a bad piece of steak, track down what object (collider) is being hit, where the code is running, etc. Also, insert Debug.Break() statements to instantly pause the editor during key moments, such as at the moment you make the raycast contact. Then you can study the scene and see where is what is why is going on. Unity is amazing for this kind of interactive debugging.

It's hell for me to figure out the error lol :) Amazing how something that looks so simple can keep me busy for hours :)

Engineering is like that... software engineering moreso. Our team spent a week (three different engineers involved at different times) tracking down a single character problem in a Facebook share... a French "e" with an accent was coming out wrong. Easy fix, right? Replace the e with the correct é and you're done!

But nope... Around and around we went, build after build after build, finally tracking down to attaching proxies to figure out what was coming over the network, and in the end it was because FB caches the share page they generate for you... so we had literally fixed our problem in five minutes a full week earlier, and yet we could not get it to work until we finally figured out "Oh, tell Facebook to clear developer cache."