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 joshg161 · Jan 24, 2017 at 07:35 AM · c#raycastdisplayinteraction

how can i display a message to the player that they can interact with an object

I am making a game and i need to have the player see that they can interact with the object, i have a raycast set up to work with a few objects and the other things but i need them to be able to hover over an intractable object and tell them they can interact with it. If anyone could help that would be amazing!

Comment
Add comment · Show 2
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 Cherno · Jan 24, 2017 at 09:33 AM 0
Share

Please provide some more detail. How should he player be notified? Should a message appear somewhere, or should the cursor change? What exactly is it that you are having problems with, the UI, the Raycasting, ...?

avatar image joshg161 Cherno · Jan 24, 2017 at 02:47 PM 0
Share

it would be just a pop up on the canvis to tell them that there is something infront of them that they can click to interact with

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by Jacky-Marley · Jan 24, 2017 at 10:23 AM

You can create a text in your canvas and access it as a GameObject. To detect if your player is in contact with the objects, use a triggered box collider so your player wont be blocked by it and Unity will detect the collision.

ex:

 GameObject text;
 
 void OnTriggerEnter ()
 {
     text.setActive(true);
     text.GetComponent<Text>().text = "press E to use";
 }

 void OnTriggerStay ()
 {
     if (Input.GetKeyDown(Keycode.E)
     {
         Debug.Log("interaction");
     }
 }
 
 void OnTriggerExit ()
 {
     text.setActive(false);
     text.GetComponent<Text>().text = "";
 }

Comment
Add comment · Show 11 · 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 joshg161 · Jan 24, 2017 at 02:47 PM 0
Share

would i use the box collider as if it were the pointing out of the player?

avatar image joshg161 joshg161 · Jan 24, 2017 at 02:52 PM 0
Share

@Jacky-$$anonymous$$arley

avatar image Jacky-Marley joshg161 · Jan 24, 2017 at 06:15 PM 1
Share

thas's in case of you want a select indicator in a game like Third Person, but if the player don't watch the object, the text appear anyway because you're in the area

if you want to make the message appear only when the player watch the object, look at the code below

avatar image Jacky-Marley · Jan 24, 2017 at 06:11 PM 1
Share
 RaycastHit myhit = new RaycastHit();
 Ray myray = new Ray();
 myray = Camera.main.ScreenPointToRay(Screen.Width / 2, Screen.height / 2);
 if (!Physics.Raycast(myray, out myhit, 1000.0f))
 {
     if( //enter condition wanted)
     {
         text.setActive(true);
         text.GetComponent<Text>().text = "text needed";
     }
     else
     {
         text.setActive(false);
         text.GetComponent<Text>().text = "";
     }
 }

in this case, when the player look the object and the distance is not far, the text will appear else, the text disappear

avatar image joshg161 Jacky-Marley · Jan 24, 2017 at 08:22 PM 0
Share

Alright that sounds good. What would i put this in? my raycast script or make a separate one and apply it to each object i interact with?

avatar image Jacky-Marley joshg161 · Jan 24, 2017 at 11:35 PM 1
Share

add this script to your player script, for each object you want to interact, add a tag like "Object" and after in the condition you can make something like

 if (!Physics.Raycast(myray, out myhit, 1000.0f))
  {
      if( myhit.gameObject.tag == "Object")
      {
          text.setActive(true);
          text.GetComponent<Text>().text = "text needed";
      }
      else
      {
          text.setActive(false);
          text.GetComponent<Text>().text = "";
      }
  }
Show more comments
avatar image joshg161 · Jan 25, 2017 at 08:18 AM 0
Share

Alright that sounds good. What would i put this in? my raycast script or make a separate one and apply it to each object i interact with?

avatar image Sad_Man30 · Aug 26, 2020 at 10:55 AM 0
Share

i want to use this in my game but my game is android rpg game and they have button. how to change the interaction using a button?

avatar image Sad_Man30 Sad_Man30 · Aug 26, 2020 at 11:56 AM 0
Share

i want to change the if (Input.GetKeyDown(Keycode.E) to using my button

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Interaction with various objects 0 Answers

Reload clips on Gun-script not applied properly 1 Answer

Clicking on an Object to Make it the Variable Target 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