Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Vince · Sep 09, 2010 at 03:08 PM · raycastcollidertriggerobjectdetection

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vince · Sep 09, 2010 at 05:46 PM 0
Share

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.

avatar image Bampf · Sep 09, 2010 at 10:04 PM 0
Share

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.)

avatar image Bampf · Sep 09, 2010 at 10:38 PM 0
Share

Thanks for correcting me. I have reworked the solution. I've also added a 2nd solution that you may prefer.

avatar image Bampf · Sep 10, 2010 at 11:12 AM 0
Share

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.

avatar image Vince · Sep 10, 2010 at 04:34 PM 0
Share

HAHAHA, i actually did the extants solution before i read this. And it worked. Great $$anonymous$$ds right?

Thanks.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges