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 InsertNamehere · Mar 15, 2015 at 07:00 PM · collidertriggerenter

OnTriggerEnter Get Key

Hey guys, i'm new to the Unity3D and i'm trying to do something. If player is in the Trigger he/she needs to press a button in keyboard in order to get in the house. I tried everything but i couldn't make it work. I'm making a 2D game with sprites.

Sorry for my bad English. Thanks!

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
3

Answer by asadi · Mar 16, 2015 at 06:36 AM

try using this code you need to have a tag "house" attached to the house

     using UnityEngine;
     using System.Collections;
 
 public class enteringthehouse : MonoBehaviour {
     
     bool enter; // boolean to determine whether you are inside the trigger or outside
         
     void Start () {
         enter = false; // before entering the trigger area
     }
     
     // Update is called once per frame
     void Update () {
         if (enter == true&& Input.GetKeyDown(KeyCode.Space)) // determine that only after the trigger is
         {                                                    // true and you press space you can enter the house
             Debug.Log ("you can enter the house");//open the door ?/ change to other scene ? up to you
                                                  // just put the code here
         } 
     }
     
     void OnTriggerEnter (Collider collider)
     {
         if (collider.gameObject.CompareTag ("house")) //entering the trigger/collider attached to the
                                                     // house. the house object must have tag named "house" 
         {                                            // with both of these enter will only be true when you are 
             enter = true;                             // inside the trigger
         } 
     }
     
     void OnTriggerExit (Collider collider)
     {
         if (collider.gameObject.CompareTag ("house")) //exiting the trigger/collider attached to the 
         {                                                // house cause enter to become false
             enter = 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 Starks72 · Jan 03, 2018 at 12:19 AM 0
Share

Very helpful answer. I used this to modify my random spawner in my game. When the player moves to an set area and right clicks the mouse, spawn a random block. When the player leaves the area the right click will no longer work. It looks like this.

alt text

screen-shot-2018-01-02-at-61546-pm.png (100.2 kB)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

Collision between objects? 2 Answers

Trigger GUI working wrong 1 Answer

Child object's collider (on a different layer) is interfering with parent Physics... 0 Answers

Can it be detect several colliders in the same gameObject? 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