Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Interaction with crosshair and keyboard

Discussion in 'UGUI & TextMesh Pro' started by FrontBackItaly, Jul 26, 2015.

  1. FrontBackItaly

    FrontBackItaly

    Joined:
    Aug 1, 2013
    Posts:
    8
    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?
     
  2. runner

    runner

    Joined:
    Jul 10, 2010
    Posts:
    865
    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.
    http://answers.unity3d.com/questions/63102/turn-off-charactercontrol-component-through-script.html

    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
     
    Last edited: Jul 26, 2015
  3. FrontBackItaly

    FrontBackItaly

    Joined:
    Aug 1, 2013
    Posts:
    8
    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.
     
  4. runner

    runner

    Joined:
    Jul 10, 2010
    Posts:
    865
    Don't forget To add mesh collider on the GameObject called Button

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

    Code (CSharp):
    1.  
    2.   using UnityEngine;
    3.   using System.Collections;
    4.  
    5.   public class Example : MonoBehaviour
    6.   {
    7.  
    8.  
    9.       Ray ray;
    10.       RaycastHit hit;
    11.  
    12.     public int num = 0;
    13.     public int numKey = 0;
    14.     public string GameWorldObjectName = "Button";
    15.  
    16.  
    17.     void Update()
    18.     {
    19.            if( num == 1 && numKey == 1) {
    20.              Debug.Log ("E And Looked At Button");
    21.          
    22.              num =0; numKey=0;
    23.            }
    24.      
    25.             if( num == 1) {
    26.              Debug.Log ("Looked At Button");
    27.          
    28.              num =0;
    29.            }
    30.      
    31.            if (Input.GetKey("e")) {
    32.  
    33.                      print("E key is held down");
    34.                      numKey = 1;
    35.            }
    36.  
    37.  
    38.           ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    39.      
    40.           Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
    41.  
    42.           if(Physics.Raycast(ray, out hit))
    43.           {
    44.  
    45.              // Debug.Log (" Looking At " + hit.collider.name.ToString());
    46.               print(" Looking At " + hit.collider.name.ToString());
    47.               if(hit.collider.name.ToString() == GameWorldObjectName)
    48.               {
    49.                 //Debug.Log (" Looking At " + hit.collider.name.ToString());
    50.                   num = 1;
    51.  
    52.               }
    53.            
    54.           }
    55.       }
    56.   }
    57.  
    58.  
    Edited Code Change
     
    Last edited: Jul 31, 2015
  5. FrontBackItaly

    FrontBackItaly

    Joined:
    Aug 1, 2013
    Posts:
    8
    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 :)
     
  6. runner

    runner

    Joined:
    Jul 10, 2010
    Posts:
    865
    I edited the code in my above message then tested and seems to be working so maybe you will find it useful.
    Good Luck.