Search Unity

Raycast only hitting collider on first camera and not second one

Discussion in 'Scripting' started by CorWilson, May 13, 2016.

  1. CorWilson

    CorWilson

    Joined:
    Oct 3, 2012
    Posts:
    95
    In my game, there are two screens each represented by two camer. The second camera holds the displays that will read touch input and have interactions. Both cameras look at the same coordinates, but using a layer system, there are exclusive rendering layers for each camera. I use the following to read touch input for objects there:

    Code (csharp):
    1. Vector3 pos = cam2.ScreenToWorldPoint(Input.mousePosition);
    2.   RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
    3.   if (hit.collider != null) {
    4.   //do stuff here
    5.   Debug.Log(hit.transform.name);
    6.  
    7.   }
    This is where the problem occurs. When I attempt to touch the object on the second camera, no feedback returns. However, if I touch the same area on the first camera, the Debug shows up. Apparently, the raycast seems to be working on cam1, despite me referencing cam2. In addition, even if I disable the cam1 object entirely and use touch on its Game view, I still get touch debugs messages from it as if it's still there. So the real problem here is, why is the touch raycast not working for on the second camera. The second camera is the only area where any touch input will be read during gameplay, so it's valuable if this works.
     
    Last edited: May 13, 2016
  2. Rebaken-Enterprises

    Rebaken-Enterprises

    Joined:
    Nov 17, 2014
    Posts:
    17
    Any luck with this?
     
  3. Adam_Benko

    Adam_Benko

    Joined:
    Jun 16, 2018
    Posts:
    105
    No solution yet ? Got exactly the same problem. Camera secCamera; just does not work for me with ScreenToPointRay
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    This is a 3 year old post, where the OP tried to do a raycast in the direction (0, 0), which is invalid.

    Unless you're making the same mistake, create a new post and post your issue.