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. Dismiss Notice

Disable Input Key while in trigger.

Discussion in 'Scripting' started by ShatterGlassGames, Nov 16, 2016.

  1. ShatterGlassGames

    ShatterGlassGames

    Joined:
    Jan 14, 2016
    Posts:
    135
    Hi guys, How can i go about disabling an Input Key like "C" when I enter a trigger and enable it when I exit?.
     
  2. Malleck666

    Malleck666

    Joined:
    Jan 9, 2015
    Posts:
    235
    You just need to check each frame, or at sensible points, if you are intersecting with that trigger. Then, change a variable based on what is returned. Your key press logic can then look for the desired value of that variable along with the input before it validates true.
     
  3. ShatterGlassGames

    ShatterGlassGames

    Joined:
    Jan 14, 2016
    Posts:
    135
    I haven't been doing program for very long so even though i can some what tell what you are saying I don't know how I would do that.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Code (csharp):
    1.  
    2. private bool canDoAction = true;
    3.  
    4. void Update()
    5. {
    6.    if(canDoAction == true && Input.GetKeyDown(KeyCode.C) == true)
    7.        DoAction();
    8. }
    9.  
    10. void OnTriggerEnter()
    11. {
    12.     canDoAction = false;
    13. }
    14.  
    15. void OnTriggerExit()
    16. {
    17.     canDoAction = true;
    18. }
    19.  
     
  5. ShatterGlassGames

    ShatterGlassGames

    Joined:
    Jan 14, 2016
    Posts:
    135
    Thank you very much :)
     
  6. Malleck666

    Malleck666

    Joined:
    Jan 9, 2015
    Posts:
    235

    Exactly that.
     
  7. AlejandroBoss10

    AlejandroBoss10

    Joined:
    Sep 1, 2017
    Posts:
    7
    I have no idea how this works. I have a similar problem but I couldn't get it to work. How did you do it. I want it to be when the trigger is entered, I can't use the Left Arrow. I tried using what is provided but I don't understand. Could you perhaps help me out?
     
  8. RodneyWheelerFilms

    RodneyWheelerFilms

    Joined:
    Dec 25, 2021
    Posts:
    2
    Did ypu ever get your answer?? I also need to find out when the player is using left arrow keys the spacebar keycode is disabled so they cannot the spam, I used Instantiate script to the spacebar keycode
     
  9. rodprogramdev

    rodprogramdev

    Joined:
    Oct 31, 2021
    Posts:
    2
    Thanks this helped me solve a bug I'm encountering :)