Interaction with crosshair and keyboard

Hi everybody, I’m currently making an FPS, but I ran into a problem. For my game, I need to use some interactable world space GUIs with only static elements like labels and images, and some buttons, so nothing complicated.

The issue is that to make the player interact with the GUI, I need to unhide and unblock the mouse from the center of the screen, which frankly is an awful way of interaction.

Since my FPS features a crosshair, made with an image centered on the screen using an overlay canvas, I was thinking about making the player interact with the GUI by simply aiming to the required button and pressing an interact button on the user’s keyboard. The problem is that I actually don’t know how to achieve this. Googling around, I read about a trick that consisted in locking the mouse to the center, and then using the left mouse click to interact with the GUI, but I sincerely don’t like this option, I think that the users would feel confused by the two functions of the same key (interact and shoot), so I’d prefer to make two different keys, so I can use the same interact key both for the GUI and the interaction with other world objects.

Another trick I read was to write a custom input module to make the Graphics Raycaster trigger when a certain key is pressed, but I have the feeling it would be just time consuming and it would make more troubles than benefits.

So, what would be the best way to make a world GUI interactable with only a crosshair and a key press?

I use a function that just flipflops a Bool that is tied too the “left or right shift keys”
hint: flipflop = !flipflop;

the Character Controller and MouseLook script’s then is enabled or disabled

The character controller disable code is similar to this code.

I do not disable the mouse cursor; The fps player can not move, The camera is locked in place and the mouse cursor still has freedom to move and interact with GUI elements

Hope that helps

I tried your solution, but frankly I don’t like the final result, I would prefer something more like Source Engine interaction, you point with your crosshair and you press E or any key you want, without ever touching your mouse to point over a button.

Don’t forget To add mesh collider on the GameObject called Button

and this should be pretty close to working code,.:slight_smile:

  using UnityEngine;
  using System.Collections;

  public class Example : MonoBehaviour
  {


      Ray ray;
      RaycastHit hit;

    public int num = 0;
    public int numKey = 0;
    public string GameWorldObjectName = "Button";


    void Update()
    {
           if( num == 1 && numKey == 1) {
             Debug.Log ("E And Looked At Button");
          
             num =0; numKey=0;
           }
      
            if( num == 1) {
             Debug.Log ("Looked At Button");
          
             num =0;
           }
      
           if (Input.GetKey("e")) {

                     print("E key is held down");
                     numKey = 1;
           }


          ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      
          Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);

          if(Physics.Raycast(ray, out hit))
          {

             // Debug.Log (" Looking At " + hit.collider.name.ToString());
              print(" Looking At " + hit.collider.name.ToString()); 
              if(hit.collider.name.ToString() == GameWorldObjectName)
              {
                //Debug.Log (" Looking At " + hit.collider.name.ToString());
                  num = 1;

              }
           
          }
      }
  }

Edited Code Change

I really appreciate your help, but I’m afraid I explained myself badly or you didn’t get my problem. Anyway, I’ll try to be as clear as possible: I have a world space GUI, and normally you should use your mouse cursor to click on its items and trigger them. With this method, you should go near the canvas, press ESC (the cursor is locked by default), click on the button, press ESC again to lock the cursor again.
Actually, I want this workflow different: it would be better if I pointed with the crosshair the button I want to press and then just press E or just any other key to trigger it, without unlocking/locking again the mouse.
And that is the problem, I don’t know how to tell Unity “Hey, the player pressed E and he’s pointing on this button, let’s press it!”.
Hope my question is more clear now :slight_smile:

I edited the code in my above message then tested and seems to be working so maybe you will find it useful.
Good Luck.