- Home /
Talking to multiple NPC's
I'm planning on having multiple characters the player could talk to, this is I'm planning on having multiple NPC scripts, each with different dialogue. Whenever the player is within range in front of an NPC, and the Player and NPC are facing eachother, a dialogue box will pop up with the push of a button. However, I want this only to occur if the player is currently looking at the front of the NPC.
The second script (Player script), uses a raycast to detect the NPC object, but it doesn't care from which side the NPC is being looked at. The reason I made this script was so that the dialogue box would only pop up when the two characters (player and NPC) are looking at each other. I was trying to reference the NPC GUI script in the Player's script, but Unity won't let me reference a boolean..
I feel like I'm missing something and going about this the wrong way. I'm open to alternate suggestions as to how to go about this. I'm still pretty new to scripting and I'm still trying to get a hang of it..
NPC script:
var target : Transform;
var guiEnabled : boolean;
var visionRange : float; //DISTANCE
var visionAngle : float; //ANGLE
//////////////////////////////////////////////////////////////
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),"blahblah");
}
}
//////////////////////////////////////////////////////////////
function Update() {
//DISTANCE
var dist : float = Vector3.Distance(target.position, transform.position);
//Vector3.Angle Example
var targetDir = target.position - transform.position;
var forward = transform.forward;
var angle = Vector3.Angle(targetDir, forward);
///////////////////////
if(angle < visionAngle && dist <= visionRange){
//print ("The other transform is behind me!");
guiEnabled = true;
}
else{
//print ("Nothin'!");
guiEnabled = false;
}
}
Player script:
var target : Transform;
//var guiEnabled : boolean;
var visionRange : float; //DISTANCE
var visionAngle : float; //ANGLE
function Update() {
//DISTANCE
var dist : float = Vector3.Distance(target.position, transform.position);
//Vector3.Angle Example
var targetDir = target.position - transform.position;
var forward = transform.forward;
var angle = Vector3.Angle(targetDir, forward);
///////////////////////
if(angle < visionAngle && dist <= visionRange){
print ("THERE!");
//guiEnabled = true;
}
else{
print ("Nothin'!");
//guiEnabled = false;
}
}
Thanks in advance!
So what is your problem? Unity will let you reference booleans in other scripts just like any other variable.
You can only work with the Boolean in the other script by referencing the instance of the other script directly.
Use GetComponent to acquire a reference to the other script. Cast it using "as NameOfScript
", and then store it into a var.
then just say varName.guiEnabled = false;
thanks for the advice, SilverTabby, but I still can't figure this situation out.. I'm not sure what you mean by casting it as "as NameOfScript" and storing it into a variable. I've been looking everywhere online and tried tackling this for several days now (I feel like an idiot), but my scripting skills are still very juvenile. Could you clarify your answer a bit more?
When I said cast it using "as NameOfScript" I meant literally copy paste it after the GetComponent I mentioned.
var npcScript = target.GetComponent("NameOfScript") as NameOfScript;
if(npcScript != null)
npcScript.guiEnabled = true;
You can also use the more confusing, but safer and faster
var npcScript = target.GetComponent.<NameOfScript>();
Answer by cookies · Aug 05, 2011 at 07:57 PM
why not have your player and npc both have raycast, and only if both are within the ray then you pop up your dialog?
that's what I've been trying to do, but I'm just not sure about the best way to go about it. If only want the dialogue box to pop up when both objects are facing eachother (so that when I wish I speak to an NPC, I won't be able to talk to their back).
Answer by Waz · Aug 05, 2011 at 09:23 PM
To access a Boolean in another object:
target.GetComponent.<NPC>().guiEnabled
(Assuming target is a GameObject or a Component subclass and NPC is the class of the script)
I don't really understand how that helps, because I don't really understand what you are trying to do, but that is how you access variables in components of other objects,
Your answer
Follow this Question
Related Questions
Getting gameObjects to lookAt using runtime csharp script 1 Answer
How to organize NPC dialogs? 0 Answers
look at player when button pressed 1 Answer
NPC LookAt smoothing 2 Answers
My Camera lookAt script doesn't work 0 Answers