Post by F1Andre on Jun 27, 2015 9:58:28 GMT
Hi, I have a simple scripting problem. You don't need any prior knowledge about UNITY.
So when the player gets close enough to an object he can press 'E' to open a menu. Doing all this is fine, but I can't figure out how to close it again. I have tried a lot of different ways to get it working. I want to make "EKeyPressedTwice" false to deactivate the menu. So when the player opens the menu he can press E again to make the menu close. The process happens to fast and Boolean EKeyPressedTwice switches between true and false to quickly to display the menu for the first time.
So when the player gets close enough to an object he can press 'E' to open a menu. Doing all this is fine, but I can't figure out how to close it again. I have tried a lot of different ways to get it working. I want to make "EKeyPressedTwice" false to deactivate the menu. So when the player opens the menu he can press E again to make the menu close. The process happens to fast and Boolean EKeyPressedTwice switches between true and false to quickly to display the menu for the first time.
float centre1;
float centre2;
private float distance;
private float maxDist = 2;
private bool isInRange;
private bool EKeyPressedOnce;
private bool EKeyPressedTwice;
//private bool keyInteract = Input.GetKey(KeyCode.E);
//public int clutch = 0;
public GameObject player;
public GameObject trigCube;
//bool but1 = GUI.Button( new Rect(centre1, 20, 100, 20), "Press E");
// Use this for initialization
void Start ()
{
//player = GameObject.Find("FPSControlle");
//trigCube = GameObject.Find ("TrigCube");
centre1 = Screen.width/2;
centre2 = Screen.height/2;
}
// Update is called once per frame
void Update ()
{
distance = Vector3.Distance(trigCube.transform.position, player.transform.position);
//Vector3 distance = player.transform.position - trigCube.transform.position;
if (distance <= maxDist)
isInRange = true;
else
isInRange = false;
if (isInRange && Input.GetKey(KeyCode.E))
{
EKeyPressedOnce = true;
}
else
EKeyPressedOnce = false;
if(EKeyPressedOnce)
EKeyPressedTwice = true;
if(EKeyPressedTwice && Input.GetKey(KeyCode.E)) // This part is the problem that needs solving
EKeyPressedTwice = false;
}
void OnGUI()
{
if(isInRange)
GUI.Label(new Rect(centre1, 20, 100, 20), "Press E");
if(EKeyPressedTwice) // Will open up the menu and do stuff while true
{
GUI.Label(new Rect(centre1, centre2, 100, 20), "Press E");
}
}