What is wrong with this hit collider script?

I am trying to get a hit collider work from the mouse click. The following script is attached to the camera:

// Update is called once per frame
	void Update () {

		RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
		
		if(Input.GetMouseButtonDown(0)) {
			print("---Mouse Down----");
			if(hit.collider != null) {
				print ("---HIT---");
			}
		}

	}

I do have BoxCollider2D active on the objects.

I haven’t really done anything with the Unity 2D object. But do you have a RigidBody2D object attached with your BoxCollider2D?

And since you want to check the collision for a point without a direction. Isn’t it easier to use Physics2D.OverlapPoint ?

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.

Try using Physics2D.OverlapPoint instead of Raycast.

would someone nice be able to show me the code for this that works?

This code should work. (untested)

Collider2D[] hits = Physics2D.OverlapPointAll(Camera.main.ScreenToWorldPoint(Input.mousePosition));

string s = "";
foreach(Collider2D hit in hits){
	s+="Hit: " + hit.ToString() + "\n";
}
if(s != "") Debug.Log(s);

There’s nothing wrong with the script. It works totally fine. All you need in your scene is a 2D collider and you’re good to go.

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.

  1. Added sprite to project
  2. Added BoxCollider2D to sprite
  3. Added tag
  4. Added the code above
  5. Added sprite to scene
  6. 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.

Here you’ll find a super simple test scene with your script:

Web Player (click your brains out)
Unity Project

Thanks Bivrost.

Uhhh still doesn’t get it to work. Here is the project i am testing with: http://www.megafileupload.com/en/file/516939/New-Unity-Project-1-zip.html

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 :slight_smile:

THANK YOU

You’re welcome. Have fun with your project :smile: