- Home /
Object detection in front of Character
I am attempting to do an Assassin's Creed/KotOR style contextual object selection in a FP camera. There are several different types of intractable objects in my scene. For each type, there is a set action that our 'action button' will perform on them. Right now I am trying to create an object detection area in front of the character/camera to populate a list of "available" objects for highlighting or some other unique identifier.
Currently I have added a game object as a child main character out in front of it and put a Capsule Collider on it, which is set to trigger. The scrip on the collider uses the "OnTriggerEnter" to make the detection of objects. This works fine, i get a list of all objects (with colliers on them) that have entered the trigger. However I am looking to refine the selection process even further by only including objects that are "visible" to the player.
Basically,I take the collider that enters the trigger, and get its local position and shot a ray from the center of the trigger to the transform of the collider that entered the trigger. This is causing problems for objects like drawers where the drawers are stacked on top of each other, and the ray hits the top drawer first, while trying to aim for the center of the second drawer (etc).
Is there an easy way to get a point on an objects surface so that I'm not firing to the middle of an objects volume?
Answer by Bampf · Sep 09, 2010 at 05:31 PM
Solution 1:
In your example, although the drawer object is large, you could specify a much smaller area, namely the handle, as being the element to test visibility on. You could even specify a single point to test, namely the center of the handle.
The handle also happens to be the thing that your character will interact directly with, if you are animating this at all.
To implement this you could model the handle as a separate object (joined to the drawer obviously). Or, you could keep the drawer and handle as a single object, while adding something to the object to indicate what spot(s) on the object should be tested.
Specifically, here is what I suggest. Add a child gameobject with a 2nd collider on it. That collider indicates small areas of the large object to test the ray against.
For simple objects like a small can, you don't need the child gameobject; the object itself is good enough. But for big objects, you can add one or more child objects that give your system specific points to check. When you hit something, if it's one of these child objects then treat it as if you hit the parent.
Basically, it's just a way for the level designer to specify points of interest.
[Edit: I reworked the above solution in response to your comment below.]
Solution 2. This is more in line with what you are asking.
Fetch the object's collider (may or may not be a mesh), and then compute points to fire rays at.
If you know it's a mesh collider, you can get the collider's mesh and then walk the vertices array of the mesh. This will be accurate but might be slow if the mesh is complex.
For all colliders you can simply get the bounds and test each corner. That's 8 tests per collider. Or, test the point that's at the center of each face of the bounding box (6 tests.) (Use the extants to compute the points on the faces.)
I would start with the bounding box method, and then for large or complex objects, either use the object's mesh collider or add one or more child mesh colliders (solution 1) to mark the areas you want it to test.
This wont work because the drawer is merely one example. There are several other objects that will be included in this list, like a stack of money, a coke can, anything like that. The trigger collider is just a collider attached to the main character and moves with him, all the other objects just have mesh colliders on them.
Just an FYI: mesh colliders might be overkill for simple objects like a can or stack of money. Not only are they more expensive, but they can't collide with other mesh colliders (last I checked.)
Thanks for correcting me. I have reworked the solution. I've also added a 2nd solution that you may prefer.
Sorry, I edited solution 2 to apply it to your question better. What you asked was more specific than what I originally answered. But I think I've fixed it now. Hope it helps.
HAHAHA, i actually did the extants solution before i read this. And it worked. Great $$anonymous$$ds right?
Thanks.
Your answer
Follow this Question
Related Questions
Detecting inside an area without a collider 1 Answer
Enable objects to enter the trigger 2 Answers
Make raycast ignore some triggers but hit some others? 2 Answers
Help with collision detection with specific colliders 1 Answer
Raycast and Hit Problem 0 Answers