Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Oct 30, 2013 at 06:09 PM by WhipJr for the following reason:

commands.close.hint.6

avatar image
2
Question by WhipJr · Oct 11, 2013 at 07:00 PM · collisiondetectionkeydown

check if player is colliding with a trigger

i need to check if The player is colliding with a trigger tagged "Dock" when they press "Q" and if its true then run "Water();" collision was never my forte so at the moment im pretty lost, especially since its been so long since ive done this. here's what i have so far:

 public class VehicleAccess : MonoBehaviour {
     
     
 public bool AccessBoat = true;
 public bool AccessCar = true;
 public bool AccessAirship = true;
 public GameObject Player;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         //If Q is pressed
         if(Input.GetKeyDown (KeyCode.Q))
         {
             //check for player's collision with game object tagged Dock
             //if true 
 
             Water();
             
         }
     
     }
     
     //If player is trying to enter the Ship
     void Water()
     {
         Debug.Log("Water");
         
     }
     
     
 }

I know its not much but as i said i was never good at collision, which is all i need to understand and implement the collision detection to move on.

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

  • Sort: 
avatar image
5
Best Answer

Answer by Tomer-Barkan · Oct 11, 2013 at 07:20 PM

Add a boolean variable, set it to true when the collision starts, and false when the collision ends.

When the player presses Q, check that boolean.

 public class VehicleAccess : MonoBehaviour {

 private bool touchingDock = false;
 
 public GameObject Player;
     // Update is called once per frame
     void Update () {
        //If Q is pressed
        if(Input.GetKeyDown (KeyCode.Q))
        {
          //check for player's collision with game object tagged Dock
          if (touchingDock)
             Water();
        }
     }
  
     void OnTriggerEnter(Collider other) {
         if (other.tag == "Dock") {
             touchingDock = true;
         }
     }
     
     void OnTriggerExit(Collider other) {
         if (other.tag == "Dock") {
             touchingDock = false;
         }
     }
 }
Comment
Add comment · Show 4 · 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 WhipJr · Oct 11, 2013 at 07:47 PM 0
Share

okay so i understand a little now, but something i noticed... do the OnTriggerEnter and OnTriggerExit of the collider only happen once?

avatar image Tomer-Barkan · Oct 11, 2013 at 07:54 PM 1
Share

Yes, once per collision. The first frame after they collide, OnTriggerEnter() is called. Then as long as they are still in collision (ie still occupy the same space), the OnTriggerStay() is called every frame (more than once probably). Last, the first frame after the collision ends (ie they no longer occupy the same space), the OnTriggerExit() method is called only once.

If after the collision ends, and the objects separated, they collide again, then all three methods will be called again in the same order, regardless of the previous collision.

avatar image WhipJr · Oct 11, 2013 at 08:00 PM 0
Share

okay, thank you, i just couldnt see because i had it collapsing in the console

avatar image Meceka · Apr 14, 2014 at 09:14 AM 0
Share

Thanks a lot, i was looking for an example for using a boolean to toggle with collider, this will help me a lot, thanks :)

Follow this Question

Answers Answers and Comments

17 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

Related Questions

collision wont work 1 Answer

Best practice for OnTriggerEnter detection 1 Answer

Collision not working 3 Answers

Detecting Trigger Collision for Mecanim Animator 0 Answers

Collision detection not working properly with 2D sprites 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