Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

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:
    50
    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:
    4
    Any luck with this?