Search Unity

What is wrong with this code?

Discussion in 'Scripting' started by herbie, Feb 27, 2014.

  1. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    In my Android game I have a script with this code:
    Code (csharp):
    1. #pragma strict
    2.  
    3. public var Collider1: Collider;
    4. public var Collider2: Collider;
    5.  
    6. public var Test1Active: boolean = false;
    7. public var Test2Active: boolean = false;
    8.  
    9. function Update ()
    10. {
    11.     var sceneCams = Camera.allCameras;
    12.     for(var thisCam: Camera in sceneCams)
    13.     {
    14.         DoCameraRayCasts(thisCam);
    15.     }
    16. }
    17.  
    18. function DoCameraRayCasts(camReference: Camera)
    19. {
    20.     var ray: Ray = camReference.ScreenPointToRay (Input.mousePosition);
    21.     var hit: RaycastHit;
    22.  
    23.     if (Collider1.Raycast(ray, hit, Mathf.Infinity))
    24.     {
    25.         Test1Active = true;
    26.     }
    27.    
    28.     if(Collider2.Raycast(ray, hit, Mathf.Infinity))
    29.     {
    30.         Test2Active = true;
    31.     }
    32. }
    When Collider1 is hit, Test1Active becomes true and when Collider2 is hit, Test2Active becomes true.
    But sometimes Test1Active becomes true when Collider2 is hit.
    Is there something wrong with this code?
    (Somewhere else in the script Test1Active and Test2Active are set to false again before you can hit a collider).
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Is it possible to hit both at once? There's nothing in there to check for that.
     
  3. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Was what I was thinking. Maybe he needs to look at hit.collider to see what was hit?
     
  4. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    No, it's not possible to hit both at once. There's a lot of space between them.

    I start to think that this code is not wrong but that something else in the script goes wrong.
    But the code above is the only part of the script where Test1Active = true.
     
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Or perform a scene raycast rather than just casting against those objects.

    Off the top of my head...
    Code (csharp):
    1.  
    2. bool didHit = Physics.Raycast(ray, hit);
    3. if (didHit) {
    4.     if (hit.collider == Collider1) {
    5.         // Blah
    6.     }
    7.     else if (hit.collider = Collider2) {
    8.         // Blah
    9.     }
    10. }
    11.  
    herbie, the difference is that you're checking a rays against specific colliders, which I'm pretty sure just returns whether an intersection exists, whereas the above casts a ray into the whole scene and then checks the collected hit info to see what was hit.

    Edit: In that case it could be worth showing the rest of the script. Nothing seems broken in what you've shown above.
     
  6. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    I changed the script as you suggested and now it's working great.
    Thanks!