Home > database >  Unity Input.GetKeyDown(KeyCode.F5) not detecting key Press
Unity Input.GetKeyDown(KeyCode.F5) not detecting key Press

Time:10-27

Unity Input.GetKeyDown and Input.GetKeyDown not detected...

In a Unity 3d script, I wrote this:

void Update (
    if (Input.GetKey(KeyCode.F5) && !aim)
    {
        StartCoroutine(ToggleAimOn());
    }
    else if (Input.GetKey (KeyCode.F5) && aim)
    {           
                    StartCoroutine(ToggleAimOff());         
    }

This part of the script allows me to display my player in 3rd person by pressing F5, only using "Input.GetKey(KeyCode.F5)" if you press F5 too long the 3rd person will "activate, disable, enable etc..." so I found this new script:

if (Input.GetKeyDown (KeyCode.F5) && !aim)
{
    StartCoroutine(ToggleAimOn());
}
else if (Input.GetKeyDown (KeyCode.F5) && aim)
{           
                StartCoroutine(ToggleAimOff());         
}

Now, if we stay pressed it puts us in the 3rd person and that's it, but when I'm in the 3rd person my character no longer performs any movement.

CodePudding user response:

First af all;


private void Update()
{
    if (Input.GetKeyDown(KeyCode.F5))
    {
        Debug.Log("F5 Pressed.");
    }
}

You may try this, problem is probably on your coroutine func. After this, I recommend use Coroutine's in GetKeyDown. Because in update processes the "coroutine" will start itself over and over again. Make sure that the "coroutine" process starts 1 time.

CodePudding user response:

Yes I think it works but how do I activate my fat cououtine at input.Getkey(down) without the couroutine activating several times (my couroutine which is used to put the camera in the 1st/3rd person) must I do

`if (Input.GetKeyDown(KeyCode.F5))

{

yield return new WaitForSeconds(0.5f);

StartCouroutine (ToggleAimOn);

}`

  • Related