- Home /
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?
What you're asking for here is kind of big. You seem to have done your research, and you sound like you know what you're talking about, so you're probably better suited to solving this problem than most of the other people here!
You could look at the ragdoll wizard, if it is possible to work out how that thing works. I think it does something similar to what you want, but with a bit of extra functionality geared towards making ragdolls (obviously).
He said nothing about ragdolls! This is not related to adding a collision at runtime! He wants to detect mouseclicks, add colliders and Ignore collisions!!!! $$anonymous$$y God, give me a break.
@syclamoth It is indeed kind of big. But since I don't know the exact problem (thus the 2 questions about colliders) or the answer (to being able to detect mouse clicks on objects) I'm hoping it's not too big to fit in an answer here! And you're right, I did my research and carefully read every link in here. ;)
So, to clarify- you need all this to work at runtime, no? I noticed that a common thread in those solutions you so kindly linked to was that they were generally editor-only scripts.
@syclamoth yep, the whole point here is doing it at runtime. Though I don't really need it to my case, it would make everything easier. Plus I don't see why this can't be done quick enough from a computational point of view. - also I just finished editing the question, hopefully making it clearer. $$anonymous$$eanwhile I am looking for ragdolls as you suggested, but it may be overkilling.
Answer by cregox · Apr 09, 2012 at 06:16 PM
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.
Answer by Clunk · Sep 28, 2011 at 12:48 AM
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.
These methods all rely on colliders existing! This is not exactly related to the question here! The question was how to add colliders to objects with code, not how to use them once they are there!
Thanks for all your effort here, Clunk, but I'm afraid @syclamoth is right. Simply adding colliders on the object doesn't work, and using meshcolliders make them really slow - because they're not primitive in the way I meant. $$anonymous$$esh colliders would only be a solution if my question #3 can be answered positively.
Answer by Clunk · Sep 28, 2011 at 01:00 AM
gameObject.AddComponent("BoxCollider");
gameObject.AddComponent("SphereCollider");
gameObject.AddComponent("CapsuleCollider"); 3
or
gameObject.AddComponent("MeshCollider").isConvex = true;
The way Q&A works, you really should delete this and edit your other answer. Just a hint! Thanks again for trying answering me so hard and, by the way, don't $$anonymous$$d someone following you and bitching - they're giving you attention and you can use that in your advantage if you think about it. ;)
Ye, I feel kinda childish for bickering back at someone bickering at me. I was very sleep deprived and was working on code for my own project all day yesterday. It's just annoying sometimes when you try to help and someone contradicts for no reason. It wasn't just this, it was a couple of my answers to other people's questions. Lol sorry for the immaturity, I am grumpy sometimes when I am tired :)