Raycast won’t work for you as written, because in 2D the ray will be constrained to the X-Y plane, and you want to cast into the screen, i.e. along the Z axis.
Thanks big mister but i could not get it to work. This is driving me crazy as i have been testing everything i can find to try to solve this “simple” thing, nothing works so i guess i do some basic mistake as i am new to Unity, from Objective-C.
I did find a US code that works in another test app i use but not when i copy it into my current test-app. I thought that if i copied the US script and copy the setup it should work but it didn’t.
function Update () {
if(Input.GetMouseButtonDown(0)) {
print("---HIT---");
var theActualMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if(collider2D.OverlapPoint(theActualMousePosition)) {
print("HIT: " + collider2D.gameObject.tag);
}
}
}
I think i have copied the sprite properties and code from the other unity app that works but just do not get a hit.
Added sprite to project
Added BoxCollider2D to sprite
Added tag
Added the code above
Added sprite to scene
Executed and clicked on sprite, nothing happen except that the “—HIT—” is displayed but not the colliderhit
Again, i am sure this is a really simple mistake, as it work in the other project, but i can’t were i do the mistake.
Set the projection of your camera from “Perspective” to “Orthographic”, then it will work.
If you want a perspective camera, you have to adjust the screen-to-world-point transformation.
Bivrost, I do not know how to thank you but you really saved my day! Not only that, i really learned something important today after quite a few hours working with this