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 mjonasz · Sep 12, 2019 at 06:51 PM · colliderontriggerstay

How would you check if a specific collier is not present?

If player1 is not present on an objective, then the objective is not "defended". I tried onTriggerExit - it works if the player leaves the objective - but not if it is destroyed.
I tried onTriggerStay and wrote the following but it gave me false when other colliders like player2 were present. How can this be solved?

   if (col.gameObject.tag = "player1")
     defended = true
     else
     defended = false
 
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
Best Answer

Answer by Anaxis_Studio · Sep 12, 2019 at 08:34 PM

So it depends on what you're looking to do but I'm assuming that it works like this:


It only takes 1 player to be inside the trigger for it to be marked as "defended". Other players coming in/out should not impact this. ie: - Player 1 enters trigger. Objective is "defended" - Player 2 enters trigger. Objective is still "defended" - Player 2 leaves trigger. Objective is still "defended".


This might not be the best way to do it but I'm writing it from memory so not tested it. Basically you want three things to happen. When a player enters the trigger the objective is marked as defended. Should a player leave the trigger will mark the objective as "undefended". This is to account for the "last" player leaving the trigger area. To get this to work you need to check if someone is still inside the trigger and set it back to true.


  • Player 1 enters trigger (Objective Defended)

  • Player 2 enters trigger (Objective Defended)

  • Player 2 leaves trigger (Objective Undefended) this will only be for a single frame.

  • Player 1 is still inside trigger. (Objective Defended)


You also need to set the player's tag to "Player" or whatever else you choose to call it. The script containing the code below should be attached to the gameobject that has the trigger.

 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Player")
     {
         Debug.Log("Objective Defended");
     }
 }

 void OnTriggerExit(Collider other)
 {
     if (other.tag == "Player")
     {
         Debug.Log("Objective Undefended");
     }
 }

 void OnTriggerStay(Collider other)
 {
     if (other.tag == "Player")
     {
         Debug.Log("Objective Defended");
     }
 }
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 mjonasz · Sep 16, 2019 at 07:44 PM 0
Share

Thanks for your help!

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

141 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 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 avatar image avatar image avatar image

Related Questions

OnTriggerStay Question 0 Answers

Audio not playing after OnTriggerStay is applied to the scripts (Driving Simulator Project) 0 Answers

Ask for trigger-collision inside Update() 1 Answer

How can I get my script to recognise when a collider exits while the box collider of the trigger object is disabled? 1 Answer

OnTrigger event when colliders are already touching eachother 1 Answer


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