- Home /
Press "E" to activate Object. Display Gui-texture or Text ??
I'm currently a high school student working on an animation/game development project, and me and my group partners are having trouble figuring out how to get what we had envisioned to work.
Our goal was to have the cursor to be hidden in our scene, and instead have a crosshair to be at the center of the screen. When the crosshair hovers over certain objects when the player moves the mouse around, we want the object's texture to change a color so the player knows its "selectable". Upon hover, a text would display in the bottom of the screen to tell the player they can press "E" to activate the object. Upon pressing "E" to activate the object, it would either display subtitles/text at the bottom center, or a guitexture/image onto the screen. (E.g. if you pressed E on a paper on a desk, an overlay of that paper would be plastered across the players screen so it can be read legibly vs seeing it from afar)
Our objects are made in Maya and imported into Unity. We are working in javascript.
We are clearly rookies, but any code or direction would be greatly appreciated. Our project is due this Wednesday afternoon, and this is the only thing left in our project that we haven't completed.
The method is something as:
Use Raycast to shoot a ray from your crosshair.
If it hits an object then change the texture of that object using $$anonymous$$aterial.SetTexture().
Also store the current texture of the object so that when it is not selected you can make that object have his original texture back.
When raycast hits an object check if it is the same object as previous before replacing texture. If not same give previous object its original texture back.
Also on raycast show the text to press $$anonymous$$
If an object is hit and user presses E key then perform the action to pick up that object.
Here Raycast and SetTexture are the key things. Rest is just plain logic. There are a lot of cases not covered in this logic above which you can as per your need. :-)
Answer by JNaski · Nov 25, 2014 at 09:48 AM
I have made something similar. You want to use Raycast and check if it hits an object.
if(Physics.Raycast (transform.position, transform.forward, hit, 5))
This is the basic check for Raycast.
Then you can for example use switch case to check what tag the object has. Then when proper key is pressed something happens.
Answer by Ellis · Nov 25, 2014 at 10:44 AM
In order to hide your cursor you type "Screen.showCursor = false". In order to know what you are looking at you'll want to use raycast as those above me has already pointed out. For the text or images to pop up you could look at the GUI and for the crosshair to change color you want to look after either a color change or a way to switch between different materials.
Here are a few links that might get you starting:
Your answer
Follow this Question
Related Questions
Make crosshairs disappear while aiming 0 Answers
Fade multiple GuiTextures in ONE Javascript 1 Answer
Removing crosshair on save 1 Answer
Shooting with a locked camera position 1 Answer
Disable player input whilst a GuiTexture is enabled? 0 Answers