Hi
I’m developing a unity android application and I want to achieve a simple thing with unity.
I want to detect which object is touched by the user, so I’ve made some reasearch on web and try to use the below solution for that
However it doesnt draw the ray with Debug.DrawRay method and Physics.Raycast method never returns true even if I’m always touching the objects.
Can somebody tell me what I’m doing wrong about that
My MainCamera has x rotation value is 40 , does it make any difference ?
Thanks
bool IsTouchedToModel(Touch touch)
{
RaycastHit rhit;
GameObject gObjectHit = null;
bool objectHit = false;
Debug.Log("Casting Ray To Point X:"+touch.position.x+" Y:"+touch.position.y);
Ray ray = Camera.main.ScreenPointToRay(touch.position);
Debug.DrawRay(Camera.main.transform.position,ray.direction*100.0f,Color.red,5.0f);
if (Physics.Raycast (ray, out rhit,Mathf.Infinity)) {
Debug.Log("Ray Shot and hit!");
objectHit = true;
gObjectHit = rhit.collider.gameObject;
sceneCreator.CheckGameObjectSelected(gObjectHit);
}
return objectHit;
}
void Update ()
{
if (Input.touchCount == 1 )
{
Touch touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
IsTouchedToModel(touch);
break;
}
}
Do you have colliders on the objects that you’re trying to Raycast? They can be set to be Trigger colliders.
How can I add colliders to my instantiated prefabs?
Other than that is Drawing ray with Debug.Drawray requires colliders? Because I cannot even draw ray
Thanks
The easiest way to make sure an instantiated prefab has a collider is to add the collider to the prefab before it is instantiated. Not sure about Debug.Drawray, have barely used that one.
How can I add a colider to prefab before instantiating?
In the Unity editor. If you’re new to prefabs and/or are using one supplied by someone else, this page has more info on creating prefabs:
my prefabs has game objects attach to them. So I thought every game object has colliders even if I dont assign one.
Am I wrong?
No they don’t, you need to manually assign a collider. 
Ok I’ll try to assign collider to each of my game object but thing that I dont understand I cannot draw ray with Debug.DrawRay method . Do I need colliders for just drawing ray also?
Or is it related with my camera position.
Can you check my first message in this forum
Thanks
No, I don’t think drawing a ray is related to colliders. It is related to the camera position, I believe the ray is drawn from the camera. I did see that part of your message but I don’t know much about drawing rays, I have never done this.
What are you hoping to use Debug.DrawRay() for? I’m pretty sure, since it’s part of the Debug class functionality, that it’s not meant as a customer-facing feature, and could possibly just display the Ray/line just in the Scene-view panel, not in the actual game.
If you’re looking for an in-game decent looking line to be drawn, you should looking into using the Line Renderer (Unity - Manual: Line Renderer component).
From your code above though, you should see a red line drawn in the Scene-view from the origin of your main camera, in the direction that it is facing, for 5 seconds…