Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Matt Woods · Sep 30, 2011 at 04:18 PM · collidertriggerintersect

When two triggers intersect, when are enter and exit events called?

So, in my game I have a number of objects that send messages to the player prompting them to take a closer look. I originally did this by having a large spherical trigger collider around the object that activated when they came close. In playtesting, people wanted that message to appear only when the player is looking at the triggering object.

At first I thought I had to do some complicated 3d math to determine the direction that the character is facing in relation to the object, then I thought of another way to do it. Instead of a big spherical trigger around the object, I instead made a small one, and attached an invisible cone shaped trigger (based on line of sight) to the player. My objects trigger checks OnTriggerEnter, and OnTriggerExit to see if the activating trigger is the line of sight cone, and if so calls the message function.

Unfortunately, this doesn't seem to be working. When my sight cone intersects the objects cone, the object receives an OnTriggerEnter, but then immediately receives an OnTriggerExit. I'm not sure why I am getting the OnTriggerExit call, as the two triggers are still intersecting. I need it to not send the exit until the player looks away, and the triggers are no longer intersecting. Any ideas what is going on, and how to work around?

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
0

Answer by aldonaletto · Oct 01, 2011 at 03:06 AM

Maybe the problem is caused by some bug in the object you're using as the conic trigger. I don't know how you can solve this, but if you want to switch back to the spherical triggers, the 3D math is very simple - attach this script to the player:

var viewAngle: float = 20; // view angle in degrees

function OnTriggerStay(col: Collider){ var tScript: TriggerScript = col.GetComponent(TriggerScript); if (tScript){ // if the trigger has a TriggerScript.js attached: var objDir = col.transform.position - transform.position; var angle = Vector3.Angle(transform.forward, objDir); tScript.inViewAngle = angle

var inViewAngle: boolean = false;

function Update(){ if (inViewAngle){ // show the message } else { // hide the message } }

// ensure message off when the player exits the trigger function OnTriggerExit(col: Collider){ if (col.tag == "Player"){ inViewAngle = false; } }

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 Matt Woods · Oct 03, 2011 at 02:44 PM 0
Share

Hi Aldonaletto,

Thanks for your answer and script. Good to know the math to do that. I ended up solving my problem by discovering the renderer.isVisible command.

I still don't know why my previous trigger code wasn't working. I think its because I had the trigger on the smaller collider, and that there is a bug/feature where "onTriggerExit" gets called on a small trigger when that small trigger completely enters a larger trigger. I'll have do more to test that theory.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can't click gameobject when over another trigger? 1 Answer

OnTriggerEnter mesh collider problem 1 Answer

Enemy line of sight using linecast and colliders 1 Answer

Rigidbody Trigger 1 Answer

GameObject Parent & Child Visibility 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