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

Bug New Input System fires twice with .started

Discussion in 'Input System' started by WakandianEngineer, Sep 8, 2023.

  1. WakandianEngineer

    WakandianEngineer

    Joined:
    Dec 24, 2020
    Posts:
    3
    so i have this two classes: Handgun fires two time if i time correctly my left mouse button, once when i press and the second when i release, i tried to use all the fix i found online none worked.
    Help me please this is driving me nut.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.     public abstract class Weapons : MonoBehaviour
    6.     {
    7.         protected Characters.PlayerCharacter _player;
    8.  
    9.         public PlayerCharacter Player { get => _player; set => _player = value; }
    10.  
    11.         protected void Awake()
    12.         {
    13.             _player = Characters.PlayerCharacter.Instance;
    14.         }
    15.  
    16.         protected void OnEnable()
    17.         {
    18.             if (_player != null)
    19.                 RegisterForShoot();
    20.         }
    21.         protected void OnDisable()
    22.         {
    23.             UnregisterForShoot();
    24.         }
    25.  
    26.         public virtual void Shoot(InputAction.CallbackContext context)
    27.         {
    28.            //blank
    29.         }
    30.  
    31.         protected virtual void RegisterForShoot()
    32.         {
    33.             if (_player.InputAct != null)
    34.             {
    35.                 _player.InputAct.Player.Fire.started += Shoot;
    36.                 _player.InputAct.Player.Fire.Enable();
    37.             }
    38.         }
    39.  
    40.         public virtual void UnregisterForShoot()
    41.         {
    42.             if (_player.InputAct != null)
    43.             {
    44.                 _player.InputAct.Player.Fire.started -= Shoot;
    45.                 _player.InputAct.Player.Fire.Disable();
    46.             }
    47.         }
    48.  
    49.     }
    50.  
    Code (CSharp):
    1.     public class Handgun : Weapons
    2.     {
    3.         public override void Shoot(InputAction.CallbackContext context)
    4.         {
    5.             if (context.started == true)
    6.             {
    7.                 Debug.Log("Pew");
    8.                 Bullet bullet = _player.BulletPool.GetChild(0).GetComponent<Bullet>();
    9.  
    10.                 Vector3 _endPosAim = new Vector3(_player.AimObject.transform.position.x, _player.AimObject.transform.position.y + 1.5f, _player.AimObject.transform.position.z);
    11.  
    12.                 //RaycastHit _hitInfo;
    13.                 //if (Physics.Linecast(_player.AimStart.transform.position, _endPosAim, out _hitInfo))
    14.                 //{
    15.                 //    _endPosAim = new Vector3(_hitInfo.point.x, _player.AimObject.transform.position.y + 1.5f, _hitInfo.point.z);
    16.                 //}
    17.  
    18.                 // Set the maximum distance and destination of the bullet
    19.                 bullet._start = _player.AimStart.transform.position;
    20.                 bullet._destination = _endPosAim;
    21.  
    22.                 // Activate the bullet game object
    23.                 bullet.gameObject.SetActive(true);
    24.  
    25.                 if (_player.VirtualCamera.GetCinemachineComponent<Cinemachine.CinemachineBasicMultiChannelPerlin>().m_AmplitudeGain < 0.3f)
    26.                     StartCoroutine(CameraShake());
    27.             }
    28.  
    29.         }
    30.  
    31.         IEnumerator CameraShake()
    32.         {
    33.             _player.VirtualCamera.GetCinemachineComponent<Cinemachine.CinemachineBasicMultiChannelPerlin>().m_AmplitudeGain = 0.3f;
    34.             yield return new WaitForSecondsRealtime(0.1f);
    35.             _player.VirtualCamera.GetCinemachineComponent<Cinemachine.CinemachineBasicMultiChannelPerlin>().m_AmplitudeGain = 0f;
    36.         }
    37.  
    38.     }
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Use
    .performed
    instead of
    .started
    . The performed event is for doing the stuff the action should. started/performed/canceled should be used when you need some "holding" mechanism, like press button, hold while a progress bar goes up and release when you're done. Like setting "power" for billiard or something. performed is for the action, started for starting animation, counter whatnot and canceled if the action was ended prematurely without firing.
     
  3. WakandianEngineer

    WakandianEngineer

    Joined:
    Dec 24, 2020
    Posts:
    3
    thank you very much, you are the best.
    i think this solution solved it, but ill run some more test before calling it close. Thank you so very much.