Question by
alagimel26 · Apr 15, 2016 at 01:23 PM ·
colliderontriggerenterkeypress
why doesn't my keypress work?
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class Displayui : MonoBehaviour {
public GameObject capsule;
public Text display;
public Text interact;
void Start () {
capsule = capsule.GetComponent<GameObject> ();
display = display.GetComponent<Text> ();
interact = interact.GetComponent<Text> ();
display.enabled = false;
interact.enabled = false;
}
void OnTriggerEnter(Collider capsule)
{
display.enabled = true;
if (Input.GetKeyDown(KeyCode.E)) {
display.enabled = false;
interact.enabled = true;
}
}
void OnTriggerExit(Collider capsule)
{
display.enabled = false;
interact.enabled = false;
}
} so Im planning to get a interact key inside a collider / OnTriggerEvent is it possible? thanks
Comment
Best Answer
Answer by Addie · Apr 15, 2016 at 01:36 PM
@alagimel26 Use onTriggerStay it is called once per frame unlike OnTriggerEnter
public GameObject capsule;
public Text display;
public Text interact;
public bool pressed;
void Start () {
capsule = capsule.GetComponent<GameObject> ();
display = display.GetComponent<Text> ();
interact = interact.GetComponent<Text> ();
display.enabled = false;
interact.enabled = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
pressed = true;
}
if(Input.GetKeyUp(KeyCode.E))
{
pressed = false;
}
}
void OnTriggerStay(Collider capsule)
{
display.enabled = true;
if (pressed)
{
display.enabled = false;
interact.enabled = true;
}
}
void OnTriggerExit(Collider capsule)
{
display.enabled = false;
Interact.enabled = false;
}
Your answer
Follow this Question
Related Questions
OnTriggerEnter Issue - Collider problem 0 Answers
OnTriggerEnter2D not fired while OnTriggerStay2D is 1 Answer
How to call an action from a script that is on the anonymous object you collide with? 1 Answer
[RESOLVED] OnTriggerEnter2D with 2 different missile types colliding with player PROBLEM 0 Answers