Raycasting problem

i’m using one of the overloads for Physics2d.Raycast and my compiler (just visual studio) doesn’t like it:

void HandleClick(Vector3 clickPos)
        {
            Ray ray = Camera.main.ScreenPointToRay(clickPos);
            //RaycastHit2D hit;
            RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
            Debug.DrawLine(transform.position, clickPos, Color.red);
            if (Physics2D.Raycast(ray, out hit, 100))
            {
                if (hit.collider.tag == "Enemy1")
                    EnterState(State.ApproachingEnemy);
            }
            else
            {
                targetPos = clickPos;
                EnterState(State.WalkingToSpot);
            }
        }

i don’t know where i got ray.origin and ray.direction from. a mouse position cannot have a direction. that’s probably what’s wrong. it gives the error at Physics2D.Raycast(ray, out hit, 100) - on the first two arguments, ray and out hit. i might be able to fix this on my own before anyone gets to it but … we’ll see.

edit: i changed hit to Physics2d.Raycast(transform.position, ray.origin).

still saying the two parameters i mentioned above are wrong. to give more detail, i am trying to cast from the hero object (to which the script is attached) to a position where the mouse is clicked.

edit2: i eventually settled with
if (Physics2D.Raycast(transform.position, direction1, 10)) …
but my debug line isn’t even drawing. it’s a small state machine, i’m not sure what’s wrong.

here’s how my code is now:

void HandleClick(Vector3 clickPos)
        {
            //Ray ray = Camera.main.ScreenPointToRay(clickPos);
            var heading1 = clickPos - transform.position;
            var distance1 = heading1.magnitude;
            var direction1 = heading1 / distance1;
            Ray ray = new Ray(transform.position, direction1);
            //RaycastHit2D hit;
            RaycastHit2D hit = Physics2D.Raycast(transform.position, direction1);
            Debug.DrawLine(transform.position, clickPos, Color.red, 10);
            //if (Physics2D.Raycast(ray, out hit, 100))
            if (Physics2D.Raycast(transform.position, direction1, 10))
            {
                if (hit.collider.tag == "Enemy1")
                    EnterState(State.ApproachingEnemy);
            }
            else
            {
                targetPos = clickPos;
                EnterState(State.WalkingToSpot);
            }
        }

There’s no overload that matches, right? I don’t see that in the docs.
I do see that you can specify the distance, so you could change your first raycast to include that.

1 Like

For one, I think “Ray” is actually for 3D … there’s Ray2D for 2D. Looks kinda like you’re mixing 2D and 3D here and there too. I mean… I think your Raycast actually looks to me like the 3D call and that doesn’t seem to be how you do it in 2D, based on the docs. Definitely check the link @methos5k recommended for the proper usage.

1 Like

fixed that (now using Ray2D), i think. still not working. my character just dances around playing the attack animations and nothing happens when i click. this will probably require a bit of tinkering.

edit: what’s weird is that the debug drawline isn’t even working. that would be a good first step, to get that working. i saw in a video that the user created two transforms and then used those in the code but what i’m doing should work, except it’s not, so i must be doing something wrong.

edit2: do you guys think it could be the depth that’s off? still the debug draw line should show a red line shooting out … ugh. this is some messy code.

Good call on Ray2D. My bad for missing that. Not sure about your line issue or why it’s not working.
Are you using just 1 raycast now, instead of 2 from before? (separate issue to the drawline).
You could debug.log some values to see if they are working.

1 Like

I’d suggest you use Debug.DrawRay instead of Debug.DrawLine, and check if your raycast is as you expect it to be. The reason for this change is because the parameters for Debug.DrawRay are more similar to what Physics2D.Raycast uses and therefore a more accurate simulation of what the raycast is doing. If the raycast is behaving as intented, then the issue is more likely a logic problem with your state machine.

1 Like

okay, i’m finding that there’s a lot wrong with my code besides just the raycasting. i had been stuck, but figuring out about raycasting seems to have got me going, i might fix the FSM soon. attacking is stuck in a loop, number one, and i wasn’t really being careful about making sure to switch between states. feels good to finally be getting work done!