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 TheShrunk · Sep 08, 2017 at 11:05 PM · collision detectiontriggers

Checking if colliding with anything in a layer

Hello to the people of unity answers!

I am trying to figure out how to make my player character in a 2D game die whenever they aren't colliding with any colliders in a certain layer, effectively creating a boundary they must stay inside. The problem is there are multiple overlapping colliders in the layer and simply killing the player upon exiting one won't work. Here is a small bit of code:

 void Update ()
     {
         if (boundariesTouching < 1 & respawning == false) //If the player is not in a boundary and not respawning then kill them.
             StartCoroutine (Respawn ());  //Teleports the player back to origional start point after short delay and animation.
     }
 
     void OnTriggerEnter2D (Collider2D collider)
     {
         if (collider.gameObject.layer == safeLayer) //If the player enters a boundary add one to the boundariesTouching variable.
         { 
             boundariesTouching++;
             print ("Player is in " + boundariesTouching + " boundaries");
         }
     }
 
     void OnTriggerExit2D (Collider2D collider)
     {
         if (collider.gameObject.layer == safeLayer) //If the player leves a boundary subtract one from the boundariesTouching variable.
         {
             boundariesTouching--;
             print ("Player is in " + boundariesTouching + " boundaries");
         }
     }

The problem with this system is that when the player falls out of the boundaries, they are teleported back to the original starting point, which for some reason only sometimes doesn't count as colliding with the boundaries, and kills the player again. The only reason I can think it does this is when the player dies and is teleported back to start, the player's rigidbody simulation is turned off for a brief time. I don't know how to stop the rigidbody from being simulated while still letting colliders function.

Other than raycasting into the layer every frame, I'm clueless here.

Comment
Add comment · Show 3
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 MacDx · Sep 08, 2017 at 11:32 PM 0
Share

Is it really necessary to have the player inside the trigger all the time and kill it when going out? Couldn't you just place four triggers to create a square and if the player touches any of those edges then he dies?

avatar image TheShrunk · Sep 08, 2017 at 11:41 PM 0
Share

It seems it would be easier to create levels by placing large boxes where they can go... but if there is no other way then I guess I'll do that.

avatar image MacDx TheShrunk · Sep 08, 2017 at 11:49 PM 0
Share

It does seems it would be easier at first glance I mean using only 1 trigger sounds better than using 4 but code wise using only 1 trigger has more edge cases to handle, if you have 4 triggers you only have to worry about OnTriggerEnter2D and that's it, no Update check and no Exit check.

1 Reply

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

Answer by TheShrunk · Sep 09, 2017 at 12:32 AM

After modifying how the player respawns I solved it. It turns out that turning off the rigidbody's simulation did indeed cause the collision to not register.

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

69 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 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 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

get only one colliding body 3 Answers

OnTriggerEnter being called multiple times 0 Answers

OnTriggerEnter called much less often after a player respawn? 1 Answer

How do I use colliders and/or triggers to end the game? 1 Answer

How do you avoid collisions when teleporting CharacterControllers? 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