M
M
Michael2018-12-02 17:21:30
Unity
Michael, 2018-12-02 17:21:30

Open panel on keypress?

Unity project, room with multiple objects. Task:
- We approach the first object, press Enter and one panel opens (with text, say).
- Approach the second object, press Enter and the second panel opens.
- etc.
How can this be implemented?
Thanks for any help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2018-12-02
@GavriKos

This is usually done by writing code.
What part of the task are you having trouble with? How to detect Enter, or what?

K
Konstantin Kitmanov, 2018-12-02
@k12th

I would venture to suggest that you want to show the player descriptions of the objects they are looking at. In this case, the problem is solved something like this:
0. On objects that can be considered, we hang a class (let's call it Describable), which contains the actual description. Also, for optimization, such items need to be placed on a separate layer.
1. We hang a script on the camera that makes a raycast in Update:

var fwdLook = camera.TransformDirection(Vector3.forward);
RaycastHit hit;
if (Physics.Raycast(camera.position, fwdLook, out hit, checkDistance, layer)) { // layer должен быть тот самый, где у нас предметы, checkDistance настроить по вкусу
    var describable = hit.transform.GetComponent<Describable>();
}

2. Check if the user pressed Enter. If clicked, then we send an event that listens to the panel with the text. In the event we pass the text, the panel displays it.
3. If in point 1 describable is null, it means that the player is no longer looking at the item, and we send an event so that the panel hides the text.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question