Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Confused · Jul 29, 2011 at 02:51 AM · playerreferencelookatnpcdialogue

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!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Waz · Jul 29, 2011 at 04:12 AM 0
Share

So what is your problem? Unity will let you reference booleans in other scripts just like any other variable.

avatar image SilverTabby · Jul 29, 2011 at 05:52 AM 0
Share

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;

avatar image Confused · Aug 05, 2011 at 06:21 PM 0
Share

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?

avatar image SilverTabby · Aug 05, 2011 at 11:19 PM 0
Share

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>();

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Confused · Aug 05, 2011 at 10:25 PM 0
Share

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).

avatar image
0

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,

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges