Auto Primitive Collider

Difficult 3 in 1 question. Sorry for so many links. Trying to keep this apparently short.

(1) I want to detect mouse clicks (or touch) on any object in the screen. Seems like we need colliders for that…

(2) Problem is I have many different kinds of objects, animated or not, none with a single primitive collider made for them.

The idea is to create those primitive colliders automagically at runtime, kinda like applying convex to the maximum.

The question is: how? I don’t think any of the scripts linked here would help much for the solution I’m looking for.

(3) Would it help somehow making the colliders ignore each other?

Found out the answer, thanks to new colleagues: Octree.

And here’s a possible implementation, from Neodrop. I’ve found it here.

I will elaborate on this later on, when I’m able to implement it.

You could use a couple approaches to detect mouseclicks. Just to detect mouse click, use function OnMouseOver()
{
//Whatever you want to happen when mouse is over object
}

to detect click

if(Input.GetMouseButtonDown(0))
{
//Whatever you want to happen when you left click
}

Attach this to a box for a test to get you started. This will detect when the mouse is hovering over that box object, and if you click on it.

function OnMouseOver()
{

 if(Input.GetMouseButtonDown(0))
 {
     Debug.Log("Mouse clicked on an object!");
 }

}

Another Approach is to attach a Javascript to the main camera, using RayCast. This works on a camera that moves with mouseLook. It casts a ray from the center of the screen.

function Update()
{
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;

 if (Physics.Raycast (transform.position, direction, hit, 1000))
{
         Debug.Log("HIT!");
    }

}

There are many other way, but I think this will give you a start. Look in Unity Script reference for more information on these techniques.

gameObject.AddComponent(“BoxCollider”);

gameObject.AddComponent(“SphereCollider”);

gameObject.AddComponent(“CapsuleCollider”); 3

or

gameObject.AddComponent(“MeshCollider”).isConvex = true;