Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 /
avatar image
1
Question by Lavir · Jun 11, 2015 at 10:46 AM · colliderinputontriggerenterguitexturegetkeydown

Input.GetKeyDown and OnTriggerEnter problem.

Hello UnityAnswers! This is my first time submitting a problem here. I am making a game where the player has to read journals and notes as a part of the game. It's in first person, and my first attempt at anything real. The problem I'm having, is that when the player enters the box collider and presses E, the texture doesn't pop up on the screen. I have two scripts, Paper.js which is attached to a box collider(trigger) that is attached to the note. PaperPopup.js is attached to an empty gameobject that holds a guiTexture component and the script itself. I can't seem to figure out a solution to this problem and any help would be much appreciated. Here are the scripts:

Paper.js

 var popupTexture : Texture2D;
 private var paperPopup : PaperPopup;
 
 function Start () {
     paperPopup = FindObjectOfType(PaperPopup);
 
     if (popupTexture == null) {
         popupTexture = renderer.material.mainTexture;   
     }
 }
 
 function OnTriggerEnter () 
 {
     Debug.Log("trigger entered");
     if (Input.GetKeyDown (KeyCode.E)) 
     {
         paperPopup.Show(this);
     }
 }
 
 function OnTriggerExit ()
 {
     Debug.Log("trigger exited");
 }

PaperPopup.js

 function Start() 
 {
     Hide();
 }
 
 function Hide() 
 {
     guiTexture.enabled = false;
 }
 
 function Show(paper : Paper) 
 {
 
     if (!guiTexture.enabled) 
     {
         var t = paper.popupTexture;
         var rect = Rect( -t.width/2, -t.height/2, t.width, t.height );
         guiTexture.pixelInset = rect;
         guiTexture.texture = t;
         guiTexture.enabled = true;
     }   
 }

As you can probably see, I have not figured out how I would get the texture off the screen once the player presses E again or leaves the collider either, so some help on that would be great too.

Thank you!

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

2 Replies

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

Answer by Lavir · Jun 12, 2015 at 06:06 PM

After coming to the conclusion that this is unsolvable with my level of coding, I went looking on the asset store. I found a great tool called "Book Reader" (5$). After downloading and implementing, my problem has been solved and I can sleep peacefully at night.

Thanks to HarshadK for his input regardless.

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

Answer by HarshadK · Jun 11, 2015 at 11:58 AM

The problem is that you are trying to check for input in the same frame into which the character enters the trigger which is very very rare to happen.

You can set a bool for when user enters the trigger and check for input inside Update() and when user presses the key check if this bool to decide if character is inside trigger or not and show him the note.

Something like:

 private triggerEntered : bool = false;
 
 function Update()
 {
     // We check if user pressed E key and character is inside trigger
     if (Input.GetKeyDown (KeyCode.E) && triggerEntered == true) 
      {
          paperPopup.Show(this);
      }
 }
 
  function OnTriggerEnter () 
  {
      // We set this variable to indicate that character is in trigger
      triggerEntered = true;
      Debug.Log("trigger entered");
   }
 
 function OnTriggerExit()
 {
      // We reset this variable since character is no longer in the trigger
      triggerEntered = false;
 }

Comment
Add comment · Show 2 · 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 Lavir · Jun 11, 2015 at 12:57 PM 0
Share

That makes sense. It detects the player entering and exiting correctly, but the note (a texture) still isn't appearing on the screen when e is pressed. I have a feeling it must be the fact that the two scripts somehow can't communicate or something. I am new to unity and program$$anonymous$$g, so it is indeed a struggle. Regardless, thank you for your input.

avatar image RedDragon2001 · Apr 15, 2018 at 10:01 PM 0
Share

This worked great for me, thanks.

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

Use Input.GetKeyDown as a function? 1 Answer

show different textures when trigged entered thrice 2 Answers

Trying to make it so if I'm toucing something and have a button down it does something 1 Answer

GUI Texture - from Box to Camera 1 Answer

Box collider doesn't work when same size as mesh. 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