I have a scene where I have an game object with sprite renderer and image attached to it. The game object has a 2D collider to. I'm trying to get a true reading when I'm clicking the image but its always false, I have no idea what em i doing wrong.
TapPos = GetComponent<Camera> ().ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0));
bool SomethingTapped(){
Vector3 CanvasTapPoint = new Vector3 (TapPos.x, TapPos.y, 10);
Debug.DrawLine (TapPos, CanvasTapPoint, Color.green, 10f);
if (Physics.Raycast (TapPos,CanvasTapPoint, 20f)) {
return true;
} else
return false;
}
PS:
The Debug.DrawLine goes right through my sprite.
The script is on the camera and the camera Z position is -10
EDIT After Andrew Griffin's response changed my code to this if anyone want to do the same thing:
public Transform ObjectTapped(){
RaycastHit2D FoundObj = Physics2D.Raycast(TapPos, new Vector2(TapPos.x, TapPos.y + 1),0f);
if(FoundObj.collider != null){
Transform obj = FoundObj.transform;
return obj;
}
else{
return null;
}
}
Look into Physics2D.Raycast. As far as I am aware, Physics.Raycast will not work with 2D colliders. You must use Physics2D.Raycast instead.