Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Grizzly · Aug 04, 2014 at 06:17 AM · guiraycastjava

How to read note by pressing "E" using RayCast only?

So here is my problem ... I made a gui texture like note that can be read when E is pressed ,but the command on read works on colider trigger script ... actually I had to make 2 scripts to do this and I tought it would be better to do it raycast way with just one script based on main camera instead on gameobjects ,however I dont know how to write this script ,basicaly I am strugling for 5 hours to make it work I tryed to modify the existing script to work on raycast and taging the object I want to put ray on (the note) but it is not working or it is not raycasting corectly (aka when shoot from distance ray will show the gui "read" but when close to object it will not ,yet distance is set very near.

I know asking for scripts is cheating in a way but I am out of options ...

Thanks whoever helps.

Basicaly I need this:

alt text

Script type - JavaScript

So far the best thing I was able to do was to make gui show on ray hit but raycasting angle was incorect or I messed up the code that I was making.

Now the best way to make it right is to say that I am not just gona copy paste the script if someone gives me as I never just did when I started programing ,very often I modify my scripts for diferent kinds of same or diferent actions with same references so it will be good for my learning.

s.png (11.9 kB)
Comment
Add comment · Show 1
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 Grizzly · Aug 04, 2014 at 06:43 AM 0
Share

I managed to find this script using unity answers and its close to what I want ,It has raycasting and I tryed and it seems to be working ,however it still uses - function OnTriggerStay(theCollider : Collider)

Now if my Player is inside the colider the note will open but if note colider is same as mesh dimensions it will just show gui but it will not open the note ,I dont get it:

Here is the script:

 var noteIsOpen = false;
 function Start()
 {
  
 }
  
 function Update()
 {
          isActive = false;
          var hit : RaycastHit;
          var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
          if (myGameobject.collider.Raycast (ray, hit,$$anonymous$$athf.Infinity))
          {
           isActive = true; 
  
          }
 }
  
 function OnTriggerStay(theCollider : Collider)
 {
   if(theCollider.gameObject.tag == "Player");
     {
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
         {
             noteIsOpen = true;
         }
             Debug.Log("Youre in the note box collider");
     }
 }
 function OnGUI ()
 {
     if(isActive)
     {
          GUI.Box(Rect(140,Screen.height-50,Screen.width-300,120),(labelText));
     }
     if(noteIsOpen)
     {
         GUI.DrawTexture(Rect(100,100,256,256), noteTexture);
         Debug.Log ("Texture of note should be showing,");
     }
 }

How do I make it not use the colider as trigger but just on raycast hit from the player to note.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Andres-Fernandez · Aug 04, 2014 at 06:56 AM

You are probably looking for this. It sends a ray from the camera through any point of the screen you want (you can use the mouse position or the center of the screen to send the ray and if it collides with the note, then you read it).

Comment
Add comment · Show 7 · 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 Grizzly · Aug 04, 2014 at 07:25 AM 0
Share

That helps for gui "read note" to apear when I hover the crosshair on the note colider ,but to actually read the note I must be inside the note colider and colider must be set to trigger however this makes the gui "read note" to apear all over the colider and not just on the mesh. to make it more understandable here is what I think of.

alt text

alt text

Now first picture shows where colider as trigger has to be for me to be able to read note on "press E" Second shows how gui with label "read note" show itself in colider where the mouse is ai$$anonymous$$g.

Now I dont want this way ,what you provided is allready in the script ,but I want it to be executed without the need for player to actually stand INSIDE of the note colider when I want to read the note.

1.png (498.8 kB)
1.png (331.7 kB)
avatar image Grizzly · Aug 04, 2014 at 07:31 AM 0
Share

What I need is a simple way to aproach the note colider wich is not a trigger but a tag ,when raycast hit taged object gui shows ,and function updates that pressing E will open the note.

avatar image Grizzly · Aug 04, 2014 at 07:50 AM 0
Share

Well since the answer isnt co$$anonymous$$g ,optional solution for this is to bring the colider as close as possible to the mesh side but leave is just enough for player to step and trigger it and it works ,kinda not the way I want but .... it runs and runs on single script ...

Back to scripting now.

avatar image Andres-Fernandez · Aug 04, 2014 at 11:48 AM 0
Share

Why don't you just set a flag for the state when you are able to read the note? For example, if you are displaying the gui note (with the raycast) set a noteReadable state or something... then if state equals noteReadable and press action button (E) then read the note. No need for triggers. Or do you want to be able to read the note even when user is not facing it?

avatar image Grizzly · Aug 04, 2014 at 12:12 PM 1
Share

I found solution for this case where there is just one note on the table for example then colider trigger way can work ,but I hope someone can help me make a script based just on raycasting a tagged object which can be activated on press #key ,while raycasting shows gui message like "read" in this case note.Because I am going to make situations where note is in a locker and if there are more notes in one locker colider trigger wont know which note I am trying to read ,so please anyone.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Gui Skin not showing 0 Answers

GUI Texture 1 Answer

Fading GUI text 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 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