- Home /
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!
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.
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;
}
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.