- Home /
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) {
GUI.skin.box.wordWrap = 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;
Debug.Log("Mouse_Enter");
}
function OnTriggerExit () {
guiEnabled = false;
Debug.Log("Mouse_Enter");
}
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!
Answer by testure · Jul 26, 2011 at 02:35 AM
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.
how would I combine Vector3.Dot() and Vector3.Distance() though? It doesn't seem to want to work for me.. (though I have the dialogue text up and running with Vector3.Dot() :) ).
you don't combine it.. you test both.
if(angle >= acceptableAngle && distance <= maxDistance)
//do stuff.
Answer by templar992 · Feb 15, 2012 at 02:42 AM
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
Answer by 2bornot2bninja · Mar 19, 2013 at 06:35 PM
Question: The GUI doesn't ever go away. Why? Is there a way to fix that?
Your answer
Follow this Question
Related Questions
talking with a npc 0 Answers
Get GUITexture to follow ScreenPointToRay ray cast 1 Answer
Making raycasts go to the centre of the screen 1 Answer
Vertical distance indication 1 Answer
How do i merge these things? 1 Answer