W
W
WasTabon2022-03-07 22:20:24
Unity
WasTabon, 2022-03-07 22:20:24

Why does a 2d ray only react to itself? Do not touch other objects?

private void Update() 
    {
        //Ray2D jumpRay = new Ray2D(transform.position, transform.right);
        RaycastHit2D hit;
        hit = Physics2D.Raycast(transform.position, transform.right, 10f);
        //Debug.DrawRay(transform.position, transform.right, Color.yellow);
        if (hit.collider != null)
        {
            Debug.Log(hit.transform.gameObject.name);
        }
    }

62265af2f0f48802858869.png
What to do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2022-03-08
@freeExec

Well, you let him out of yourself, so he will meet his collider first, and only then another, but of course it doesn’t matter anymore, it returns the first one.

W
WasTabon, 2022-03-08
@WasTabon

RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right, 30f);
        if (hit.collider != null)
        {
            Debug.Log(hit.collider.gameObject.name);
        }

And choose this layer 62275d6ab18cf604381501.png
. I don’t know how correct, but it works for me

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question