Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Two LayerMasks, Only One Registers??

Discussion in 'Scripting' started by renman3000, Jan 20, 2015.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Hi,
    I am sorry for posting, but I have an issue with a simple raycast.

    I am checking a cast off of two layer masks
    Code (csharp):
    1.  
    2.         if(Input.GetMouseButtonUp(0)){
    3.               Ray ray = Cam_Main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y - cam_trns.position.y, -cam_trns.position.z));
    4.               Vector3 dir = new Vector3(ray.direction.x * 100, ray.direction.y * 100, ray.direction.z * 100);
    5.             RaycastHit hit;
    6.             if(Physics.Raycast(ray.origin, dir, out hit, Mathf.Infinity, playerLayerMask)){                
    7.                 print("playerHit");
    8.                 return;
    9.               }    
    10.             if(Physics.Raycast(ray.origin, dir, out hit, Mathf.Infinity, gridLayerMask)){
    11.                 print("floor hit");
    12.                 return;
    13.               }
    14.         }  

    The first works fine, the second never registers. The layer masks and layers are set correctly. The collider of the second is set correctly. I am drawing a ray to check the cast, it appears to pass thru said collider.


    Any ideas??
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You have returns in your if statements. If the first one is true then it hits the return and never checks the second one.

    I would also combine the masks into a single mask, do one raycast, and check the layer of the thing that got hit instead of doing two raycasts.
     
  3. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    You are multiplying dir with 100. The documentation doesn't mention the requirement of the direction to be normalised but I bet it has to be. You can put the 100 to the argument list of Physics.Raycast() instead of Mathf.Infinity and it still should do what you want. (But I might be wrong, it's just an idea.)
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It doesn't. An un-normalized vector can still represent a direction. I'm not saying it'll work the way you expect, but it will work :)
     
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Thanks, but that does nothing.

    ??

    I placed the layerMask that was working and included the the other layer. I am also just using ray.direction. It still works when I hit the player, not the floor tho. Logic would suggest it is hitting another collider first?

    EDIT
    This is so weird. I duplicated my player, and swapped his layer to the floor and it hits.

    ???
     
    Last edited: Jan 20, 2015
  6. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    Code (CSharp):
    1. Ray ray = Cam_Main.ScreenPointToRay(new Vector3(
    2.     Input.mousePosition.x,
    3.     Input.mousePosition.y - cam_trns.position.y, // Why are you doing this?
    4.     -cam_trns.position.z) // And why are you doing this?
    5.  
    Are you really hitting the object in question?

    Edit: You might want to use Unity.DrawRay to be sure.
     
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    I was using that as a way to offset cam pos. Turns out, it seems that I dont have to.

    I am not sure what you mean. Am I hitting the object in question.


    Here is pic. It shows the floor which is not resullting in a return, despite being on the correct layer. It shows two ZomBunnies (players), however one is set to floor layer and it returns as if it were a floor. I then set the floor to the player layer, thinking maybe that would return but no.



    For some reason, my floor, set to the floor layer (gridLayerMask in example) will not return. It is a cube, primitive, stretched out. Fully in view of cam and according to the drawn ray, hitting. But not registering.


    ??
     
  8. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    By asking if you are really hitting your object, I wanted to know if the ray you are creating is correctly positioned. If you are able to hit the bunnies, the ray seems to be (somewhat) correct. I've noticed another thing now. The "isTrigger" checkbox of your floor seems to be active. What happens if you disable it?
     
  9. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Oh my god.
    It works!!!!!

    Crazy, I did not realize a trigger did not fire. I thought the bunnies were triggers.

    ok.
    THANKS!!!
     
  10. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    You're welcome! :)
     
    renman3000 likes this.