Talking to an NPC

How do I make an object active only when I am simultaneously looking at it and pressing a button?
I am trying to make an NPC, basically.

So far I’ve made a few scripts, on that activates a GUI text box upon entering a trigger, and one that prints a message when you walk in front of an object (using Vector3). But I don’t know how to piece the scripts together and make it work only upon the push of a button.

GUI trigger enter script:

 var guiEnabled : boolean;

function OnGUI() {
if (guiEnabled) { = true;  
GUI.color.a = 1;
GUI.Box(Rect(10,Screen.height/2+115,Screen.width-15,Screen.height/2-120),"Talking Talking Talking.");

function OnTriggerEnter () {
guiEnabled = true;	

function OnTriggerExit () {
guiEnabled = false;	

Sense when an object is in front script:

var you : Transform;
var vision : float;

function Update () {
    var fwd = transform.TransformDirection (Vector3(-1,0,0));
    if (Physics.Raycast (you.position, fwd, vision)) {
        print ("There is something in front of the object!");
    else {
    	print ("There is nothing.");


I’m thinking that something along the lines of the NPC having a trigger object attached to its head (to represent its line of site), so that you can only talk when the person is looking at you. But again I only want to be able to interact with the NPC when the player’s camera is FACING the NPC (and if all of this is true, pressing a button will make text show up).

Hope this doesn’t sound too confusing… Thanks in advance!

Honestly you've basically got it. The only thing I would do instead of a raycast is get the dot product of the target and player to see if they're within an acceptable angle, and then test the distance to make sure it's reasonable. Raycasts are very precise, so for what you're trying to do you may end up with it rapidly going in and out of 'range'. an angle/distance is much more forgiving.



Well here’s a simple one. When you go near the box they will talk to you
Just like in skyrim here’s the script :

Var Soundtoplay:Audioclip;

function OnTrggerEnter () {
audio.PlayOneShot (SoundToplay);

After you done that place it into any object then untick play on
Awake other wise it will play automaticaly not when Ur near the box or object

Hope it works if it doesn’t work check the spelling and the space

Question: The GUI doesn’t ever go away. Why? Is there a way to fix that?