So my problem is I'd like to have one GameObject with multiple "OnMouseDown" functions. I have a target (classic looking bullseye) and I'd like to award a player say 10 points if they click on it, but 100 points if they click the center. Because the "OnMouse" functions don't pass in a collider I'm not sure how to go about doing this. I had an idea (which I think is a bad one) which would be to attach a small "empty" gameobject to my target prefab and give it a smaller hitbox with code that spits out 100 points instead of 10 and place that object "in front of" the other one while being attached. Again, seems wrong. Anybody else have to do this before or have a more elegant solution?? Thanks!


1 Answer 1


OnMouseDown is triggered when an object is clicked. If you have a bullseye like object, simply test to see how close to the center the click was using a piecewise function. This means you only need to have one object and one script. A script with something like the following:

if(distanceToCenter > 10)
   return 10;
if(distanceToCenter > 5)
   return 25;
if(distanceToCenter <= 1)
   return 100; //bullseye!

You can calculate distanceToCenter with something like:

//Calculate the mouse position in the game world. Get the screen coords and the depth
// into the scene by calculating the distance between the object and the camera.
Vector3 mouseScreenPosition = Input.mousePosition;

mouseScreenPosition.z = 
    Vector3.Distance(Camera.main.transform.position, this.transform.position);

Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(mouseScreenPosition);

float distanceToCenter = Vector3.Distance(this.transform.position, mouseWorldPosition);

All this code would go inside the OnMouseDown method, so you know the mouse is in the correct place when calculating the distance. Alternatively, you can cast a ray and use the point variable from the RaycastHit class.

  • \$\begingroup\$ Wow! Ok perfect, I'll implement this when I get home. I'll mark this answer and close this once that's done, in case for whatever reason I run into a problem and have a question (Might as well not open a new thread). Thank you so much! \$\endgroup\$ May 12, 2014 at 16:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .