- Home /
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:
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.
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.
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).
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.
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.
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.
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.
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?
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.
Your answer
Follow this Question
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