Search Unity

SOLVED: Raycast not working on Android only

Discussion in 'Android' started by peaveyyyy, Jul 22, 2015.

  1. peaveyyyy

    peaveyyyy

    Joined:
    Mar 10, 2014
    Posts:
    14
    Sorry for the cross-post, but I didn't see this forum till now and it is way more suitable

    I have a raycast which checks if a linerenderer hits an object.

    It works if I build it under Windows or Mac standalone. It works in the editor under Android but it doesn't work when built under Android.

    Anyone got any suggestions? I have searched for this issue and I have found several threads talking about colliders not working due to physics time issues, but my raycasts CANNOT MISS! The raycast is always aimed directly at the target, and often the target is not moving.

    I have tried running the raycast 5 times each time instead of once, no effect.

    The code is here but remember it works all the time except when deployed on Android
    Code (CSharp):
    1. RaycastHit2D hit = Physics2D.Raycast(btp, transform.up, LaserRange);
    2.         if (hit)
    3.         {
    4.          
    5.             Destroy(hit.collider.gameObject);
    6.           }
    I am using an Huawei Honor 6 running Android 4.4.2. If you need more info just ask. I am not an Android expert, so be kind....
     
  2. Woyzeck1

    Woyzeck1

    Joined:
    Jan 15, 2013
    Posts:
    10
    Hi. In answer to your question on my post, my problem was that the raycast object wasn't attached to a camera in my scene, it was attached to the OVR controller (for Samsung Gear VR), and because I was rendering monoscopically the app was ignoring the OVR controller and just rendering one of the cameras (the right one I think). When I attached it to my camera, rather than the controller, it worked straight away, both in the editor and in the app.

    The only thing that I can suggest, as I am not an expert, is to try what happens to the collider, so rather than
    Code (CSharp):
    1. if(hit)
    try

    Code (CSharp):
    1. if(hit.collider == someCollider)
     
  3. peaveyyyy

    peaveyyyy

    Joined:
    Mar 10, 2014
    Posts:
    14
    Thanks for your response. I will try and mess around with it a bit, maybe attach it to a dummy camera that I don't use.

    One question: Why would if(hit) be inferior in this instance?

    thanks again
     
  4. Woyzeck1

    Woyzeck1

    Joined:
    Jan 15, 2013
    Posts:
    10
    No problem.

    I might be confusing myself between raycast and raycast2d, and I only suggested it as that is what I did with my solution and it worked, although my app was in 3d space and not 2d space.
     
  5. peaveyyyy

    peaveyyyy

    Joined:
    Mar 10, 2014
    Posts:
    14
    I still have no solution so I am think maybe I need to file a bug report?
     
  6. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    292
    I have Physics.Raycast working on Android with Unity 5.1.2f1, but it also worked in earlier versions.

    My code:


    Code (CSharp):
    1.  
    2. LayerMask lm = 1 << 10;
    3. Ray ray = new Ray(_cam.transform.position, _cam.transform.forward);
    4. RaycastHit hit;
    5. float distance = 5;
    6.  
    7. if(Physics.Raycast(ray, out hit, distance, lm)) {
    8.     //do stuff here
    9. }
     
  7. peaveyyyy

    peaveyyyy

    Joined:
    Mar 10, 2014
    Posts:
    14
    Thank you Piflik.

    Is using a LayerMask the thing that makes the difference? I will try it and find out.

    I appreciate your help
     
  8. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    292
    The LayerMask is optional. The code worked without, but I added it for efficiency.

    Not sure if that is the problem, but the main difference is that I use the Physics.Raycast directly in the if clause, while in your code it is first saved to a RaycastHit variable.
     
  9. peaveyyyy

    peaveyyyy

    Joined:
    Mar 10, 2014
    Posts:
    14
    Thank you for that insight, I will report back
     
  10. peaveyyyy

    peaveyyyy

    Joined:
    Mar 10, 2014
    Posts:
    14
    Thanks to everyone who tried to help.

    My code was fine, but it needed to be in FixedUpdate(), not in Update(). I don't know why.

    It works now.
     
  11. ixikos

    ixikos

    Joined:
    Jun 21, 2013
    Posts:
    26
    Thank you, this is very strange but helped me out a lot. I had to move code out of Coroutines to fixedUpdates to get it to work properly.
     
  12. swapolicious

    swapolicious

    Joined:
    Mar 13, 2015
    Posts:
    1
    peavey dude you saved me hours man. I used LastUpdate instead of Update to make the build work in both MacOS and iOS. So thank you!