0
\$\begingroup\$

It's working fine right now, showing the text and then press the key and it takes the item but, i want something like resident evil, the text is not showing until you press acction button. How can i do that?

The key script:

var TheKey : GameObject;
private var playerNextToKey = false;
//var showMessage : boolean = false;
//var nearKey : boolean = false;
 var hasCollided : boolean = false;
 var labelText : String = "";

 function OnGUI()
     {
         if (hasCollided ==true)
     {    
          GUI.Box(Rect(140,Screen.height-50,Screen.width-300,120),(labelText));
     }
 }


function Update () 
{
    if (Input.GetKeyDown(KeyCode.E) && playerNextToKey == true)
    {
    TheKey.active = false;

    }
}

function OnTriggerEnter (theCollider : Collider)
{
    if (theCollider.tag == "Player")
    {
        //nearKey = true;
        playerNextToKey = true;
        hasCollided = true;
        labelText = "¿Vas a coger la llave?";

    }
}

function OnTriggerExit (theCollider : Collider)
{
    if (theCollider.tag == "Player")
    {
        //nearKey = false;
        playerNextToKey = false;
        hasCollided = false;

    }
}
\$\endgroup\$
1
\$\begingroup\$

So the conditions under which the dialog is supposed to pop up are:

  1. The player-character is inside the trigger-area AND
  2. The player holds down the E key AND
  3. The dialog is not already open

Condition 1 can be checked by implementing the other two conditions in the OnTriggerStay event. This function is called during each game-tick where the object is inside a trigger-collider.

You already figured out how to check the second condition (Input.GetKeyDown(KeyCode.E))

The third condition can be solved with a boolean flag.

The whole code for OnTriggerStay would look something like this:

var dialogOpen : boolean = false;

function OnTriggerStay (theCollider : Collider)
{
    if (theCollider.tag == "Player" && Input.GetKeyDown(KeyCode.E) && !dialogOpen )
    {
         ... your code to make the dialog window appear...

         dialogOpen = true;
    }
}

The next thing you might want to implement is to disable all other player input controls if dialogOpen == true, so the player is forced to interact with the dialog window and can not just walk away from it.

Then you should implement the logic behind the dialog itself. Both accepting and rejecting should hide the dialog and set dialogOpen = false so the player can continue playing and pick up another item.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.