Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Keeping player active.

Discussion in 'Scripting' started by blindstrom, May 22, 2024.

  1. blindstrom

    blindstrom

    Joined:
    Feb 4, 2016
    Posts:
    3
    The problem with the code is that the console is picking up the 'E' input multiple times per frames instead of only picking it up once. Basically, the code, when clicking 'e' will make the character smaller and I want it to stay like this, but it is flickering between large and small. As well the white blocks by the teleporters not setting active again after 'E' is pressed a second time.
    Can anyone assist with this?




    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PowerUp : MonoBehaviour
    6. {
    7.     private bool isSmall = false;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.      
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         if (Input.GetKey (KeyCode.E))
    18.         {
    19.             Debug.Log("E WAS PRESSED");
    20.             checkForSize();
    21.          
    22.         }
    23.     }
    24.  
    25.     private void checkForSize()
    26.     {
    27.          if (isSmall == false)
    28.             {
    29.                 transform.localScale = new Vector3(1f,0.5f,1f);
    30.                 isSmall = true;
    31.                 Debug.Log("BECOME SMALL");
    32.                 GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("TeleportBlock");
    33.                 foreach (GameObject go in gameObjectArray)
    34.                 {
    35.                     go.SetActive (false);
    36.                     Debug.Log("Objects GONE");
    37.                 }
    38.             }
    39.             else
    40.             {
    41.                 if (isSmall == true)
    42.                 {
    43.                     transform.localScale = new Vector3(1f, 1f, 1f);
    44.                     isSmall = false;
    45.                     Debug.Log("BECOME BIG");
    46.                     GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("TeleportBlock");
    47.                     foreach (GameObject go in gameObjectArray)
    48.                     {
    49.                     go.SetActive (true);
    50.                     Debug.Log("Objects BACK");
    51.                     }
    52.              
    53.                  
    54.                 }
    55.             }
    56.             // CODE BAD FIND A WAY TO RUN E OUTSIDE UPDATE AS IT IS
    57.             //RUNNING TOO MANY TIMES AND IF CAN REMOVE DELAY AND PLACE CODE BACK IN INPUT
    58.     }
    59. }
    60.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,391
    blindstrom and MCF24 like this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    No, it runs once per frame but it runs consecutively over as many frames as the button remains pressed. At 60 fps each Update „lasts“ for 0.016666 seconds - no human can easily push and release a button within that tiny time window.
     
  4. TeaJunkie

    TeaJunkie

    Joined:
    Nov 23, 2016
    Posts:
    24
    Input.GetKey(Keycode.E) is your problem.

    GetKey = Continuous press
    GetKeyDown = Once When Pressed
    GetKeyUp = Once When Released.

    Change to GetKeyDown to only call once per button press.
     
  5. blindstrom

    blindstrom

    Joined:
    Feb 4, 2016
    Posts:
    3
    Thanks heaps for this... Makes better sense now.
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,682
    Alternatively if you want to use the new input system's direct workflow:
    Code (csharp):
    1. using UnityEngine.InputSystem;
    2.  
    3. Keyboard.current[Key.Space].isPressed;
    4. Keyboard.current[Key.Space].wasPressedThisFrame;
    5. Keyboard.current[Key.Space].wasReleasedThisFrame;
     
    blindstrom likes this.