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
0
Question by boddole · Dec 06, 2014 at 03:39 AM · triggerboundsontriggerstay

Getting Objects Inside a Trigger (without OnTriggerStay)

Hello everyone, I've got a situation where Player/NPC boats are being instantiated, and I would like for them to do a check when created that looks for any objects of interest for each ship gun. The trouble is that there is a chance that potential target objects will be within a FOV trigger mesh that each gun has, thus bypassing OnTriggerEnter/Exit.

I've considered a few options:

Ideas I've considered: 1) Lists of Objects

My choice so far, create a list for NPC/Player objects and use a coroutine for each gun to periodically run through and choose a target from that list. Would need to make sure some sort of logic tests (followed by bounds.contains with the point) are done to minimize guns from checking against obviously impossible target objects.

2) OnTriggerStay:

Would require a bool or other simple test to prevent it from doing anything after a very brief initial amount of time to acquire objects when instantiated. Would need to make sure that the same objects are not added multiple times. And even with basic tests, if a lot of targets are inside a FOV mesh, that could still lead to lots of unnecessary work each physics update.

3) Spherecast / Capsulecast:

Would need to make sure objects don't get added multiple times, the shapes really don't match the FOV mesh very well, spheres/capsules may miss objects or hit unwanted objects.

4) Bounds.contains:

Only takes a vector3 point, could repeat the test multiple times but at that point I'm already more/less doing idea 1.

That is where am at this point. If I'm not thinking of another way of doing this, please let me know. Thank you for reading.

Comment
Add comment · Show 1
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 quansatthu · Dec 06, 2014 at 08:19 AM 0
Share

So why don't you use SphereCast? I think it's the best option for this case.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by bubzy · Dec 06, 2014 at 05:31 AM

i would say a spherecast is a good option, you can add a boolean to each object that you are detecting, and set it to true when its detected, then you can add code to only add the object if the boolean is false, that will avoid multiple detections, also you can set up collision layers to ignore certain objects.

http://docs.unity3d.com/ScriptReference/Physics.SphereCast.html

http://docs.unity3d.com/ScriptReference/LayerMask.html

layermask is important, because its not just a case of typing in the layer number on the spherecast (as i thought initially :P)

Comment
Add comment · Show 1 · 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 boddole · Dec 06, 2014 at 10:39 AM 0
Share

@quansatthu and bubzy:

$$anonymous$$y concern on the spherecast is the mismatch between the shape of the sphere vs the shape of the FOV mesh. Granted, I could do a Bounds.contains after a object is detected...

alt text

fovexample.jpg (91.6 kB)
avatar image
0

Answer by MrSoad · Dec 09, 2014 at 05:15 PM

You can try this method if you want to :

If you scale the FOV Trigger(or a similar duplicate) from very very small to the size you want then it will detect all the objects within it provided you have a rigidbody on the FOV Trigger and the objects you want to detect are setup in a way that is reciprocal to this. Do a test and output the names or tags of the objects that are detected to make sure it is picking up everything that you want it to.

Comment
Add comment · 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

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

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Mouse click breaks trigger variable C# 1 Answer

A node in a childnode? 1 Answer

Loop Sounds when within trigger 1 Answer

Save Position script. 2 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