Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Detecting when controller buttons are held down.

Discussion in 'Editor & General Support' started by Mr_AgentFox, Jul 21, 2019.

  1. Mr_AgentFox

    Mr_AgentFox

    Joined:
    Jun 23, 2019
    Posts:
    59
    Hey! I am wondering how do detect when a controller button is held down, I want to rapid-fire a gun if it is. Any answers?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     public float runSpeed = 5f;
    9.     public float shiftSpeed = 10f;
    10.  
    11.     PlayerControls controls;
    12.  
    13.     public GunScript shoot;
    14.     public BoxCreator cubeSpawn;
    15.     public GrenadeThrower throwGrenade;
    16.  
    17.     Vector2 move;
    18.     Vector2 rotate;
    19.     Vector2 sprint;
    20.  
    21.     void Awake()
    22.     {
    23.         controls = new PlayerControls();
    24.  
    25.         controls.Gameplay.Movement.performed += ctx => move = ctx.ReadValue<Vector2>();
    26.         controls.Gameplay.Movement.canceled += ctx => move = Vector2.zero;
    27.  
    28.         controls.Gameplay.Rotation.performed += ctx => rotate = ctx.ReadValue<Vector2>();
    29.         controls.Gameplay.Rotation.canceled += ctx => rotate = Vector2.zero;
    30.  
    31.         controls.Gameplay.Shoot.performed += ctx => Shoot();
    32.  
    33.         controls.Gameplay.SpawnCube.performed += ctx => SpawnCube();
    34.  
    35.         controls.Gameplay.Grenade.performed += ctx => ThrowGrenade();
    36.  
    37.         controls.Gameplay.Sprint.performed += ctx => Sprint();
    38.         controls.Gameplay.Sprint.canceled += ctx => NoSprint();
    39.     }
    40.  
    41.     void Update()
    42.     {
    43.  
    44.         transform.Translate(new Vector3(runSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, runSpeed * Input.GetAxis("Vertical") * Time.deltaTime), Space.Self);
    45.  
    46.         Vector2 r = new Vector2(-rotate.y, rotate.x) * 100f * Time.deltaTime;
    47.         transform.Rotate(r, Space.Self);
    48.     }
    49.  
    50.     void OnEnable()
    51.     {
    52.         controls.Gameplay.Enable();
    53.     }
    54.  
    55.     void OnDisable()
    56.     {
    57.         controls.Gameplay.Disable();
    58.     }
    59.  
    60.     void Shoot()
    61.     {
    62.         if (shoot.currentAmmo <= 0)
    63.         {
    64.             return;
    65.         }
    66.  
    67.         shoot.Shoot();
    68.         shoot.currentAmmo--;
    69.     }
    70.  
    71.     void SpawnCube()
    72.     {
    73.         cubeSpawn.SpawnCube();
    74.     }
    75.  
    76.     void ThrowGrenade()
    77.     {
    78.         if (throwGrenade.grenadeAmount <= 0f)
    79.         {
    80.             return;
    81.         }
    82.  
    83.         throwGrenade.ThrowGrenade();
    84.         throwGrenade.grenadeAmount--;
    85.     }
    86.  
    87.     void Sprint()
    88.     {
    89.         runSpeed = shiftSpeed;
    90.     }
    91.  
    92.     void NoSprint()
    93.     {
    94.         runSpeed = 7f;
    95.     }
    96. }
    97.  
     
  2. Same as everything else. Create a bool variable, replace the Shoot.performed to += ctx => shooting = true;
    then Shoot.canceled += ctx => shooting = false; And then check if the shooting boolean is true somewhere in the Update method and calculate if you want to shoot this frame (depends on the firing rate). If you want to keep the old Shoot() method as well, after firing you can just set the shooting boolean to false, so single shot.