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
1
Question by Confused · Jul 26, 2011 at 02:33 AM · guiraycastvectornpc

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!

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Vector3.Dot()

Vector3.Distance()

Comment
Add comment · Show 2 · 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 · Jul 26, 2011 at 07:20 PM 0
Share

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

avatar image testure · Jul 27, 2011 at 04:22 AM 0
Share

you don't combine it.. you test both.

 if(angle >= acceptableAngle && distance <= maxDistance)
     //do stuff.
avatar image
0

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

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
avatar image
0

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?

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

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


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