When I run on unity editor, the raycast works perfectly. But when I run on mobile during "Build and Run" the ray cast don't hit anything. I know it doesn't hit anything because I placed a 3dtext to denote if there is a hot or not.
I have used the 3d text to compare the rays being produce in Unity editor and phone. They produce the same rays.
I have used this code before and it was doing fine. It works in unity editor but not on phone. Am I missing something?
I am using a Samsung S10 for testing.
//Get input
if (Input.touchCount > 0 || Input.GetMouseButtonDown(0))
{
if (!IsCurrentlyInTouch)
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
ray = Camera.main.ScreenPointToRay(touch.position);
}
else
{//keyboard
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
}
RaycastHit hit;
if (Physics.Raycast(ray, out hit,Mathf.Infinity))
{
selectedObject = hit.transform;
//Do things
}
if (hit.collider != null)
{
DebugTxt.text = hit.collider.name;
}
else
{
DebugTxt.text = "no hit " + ray.GetPoint(10) ;
}
}
IsCurrentlyInTouch = true;
}
else
{
IsCurrentlyInTouch = false;
}