0
\$\begingroup\$

I'm currently using a Raycast both for my movement check aswell as a focus-setting check, both checks use pretty much the same code. The problem i'm having is that the Raycast goes through some objects and targets the background no matter what i've tried to far. As you can see in this screenshot, I first clicked on the black sphere (Just a 3D Sphere in my 2D world with a default Sphere Collider) and the Log shows so aswell, however when i click on the white box which the second Debug Log is supposed to be, it just ignores the box collider and goes straight to the Background (the green sprite). The same problem with the Interactable check, code works completely fine with the Sphere but not with the box. Here's the Code i currently use:

    //Movement
    if (Input.GetKeyDown(KeyCode.Mouse0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 100))
        {
            Debug.Log("We hit " + hit.collider.name + " " + hit.point);
            RemoveFocus();
            targetPosition = hit.point;
        }
    }
    if(targetPosition != (Vector2)transform.position)
        transform.position = Vector3.MoveTowards(transform.position, targetPosition, maxSpeed * Time.deltaTime);


    //Check for Interactable, pretty much same code...
    if (Input.GetKeyDown(KeyCode.Mouse1))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 200))
        {
            Interactable interactable = hit.collider.GetComponent<Interactable>();
            if(interactable != null)
            {
                SetFocus(interactable);
            }
        }
    }
\$\endgroup\$
  • \$\begingroup\$ Is your 2D stuff sprites, or UI? Does your 2D stuff use Physics2D? docs.unity3d.com/ScriptReference/Physics2D.Raycast.html \$\endgroup\$ – MichaelHouse May 1 '18 at 2:29
  • \$\begingroup\$ A screenshot to the components of the white box is in the text, here is the sphere which works as i intend it to. \$\endgroup\$ – Veineless May 1 '18 at 3:01
  • \$\begingroup\$ I've tried playing around with different colliders on the white box, using 3d and 2d colliders etc however nothing seemed to fix the issue, not really sure where to look anymore :( \$\endgroup\$ – Veineless May 1 '18 at 3:14
1
\$\begingroup\$

Few things:

  • It can be because your gameObject is on Ignore Raycast layer, so it automatically ignores the raycast.
  • Maybe the size of your collider is too small, so you thought you clicked on it, but you actually didn't.
  • If you only check the result by debugging to console, I notice there is no Debug.Log () line under Input.GetKeyDown(KeyCode.Mouse1)
  • You used 2DCollider, in which you need to use RaycastHit2D instead of RaycastHit. Or just just change the 2D collider to 3D collider.
  • The MaxDistance of your raycast is 100 and 200, but maybe your gameObject is farther away.
  • The collider might be too big that the camera ended up inside the collider, and because the raycast comes from camera, the collider never picked it up.
  • You might be trying to raycast UI with ScreenPointToRay.
  • If you're using sprites, the sprite that you want to raycast might be behind the background, it just seems it's infront of it because the layer order is higher.
\$\endgroup\$
  • \$\begingroup\$ I'm honestly not really sure why it didn't work before, but i tried changing it to a 3D collider again and it works perfectly fine now. If i wanted to change to RaycastHit2D instead to make things easier, how would i go about changing my script since the Physics.Raycast won't work with my ScreenPointToRay and RaycastHit2D anymore? \$\endgroup\$ – Veineless May 1 '18 at 5:13
  • \$\begingroup\$ forum.unity.com/threads/… \$\endgroup\$ – Momok987 May 1 '18 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.