Unity New Input System .started doesn't work but .performed works twice

Viewed 1301

I'm creating a shooting angry birds style game with new input system.

I want to save the mouse position to a variable when the mouse is clicked and released.

Subscribing to the .started event doesn't work. Using .performed, it works with 1 click 2 times, once at the start and once after release.

When I try .canceled it doesn't work too.

    private void OnEnable()
    {
            controls.Gameplay.Shoot.started += _ => PlayerAimingStart();
            controls.Gameplay.Shoot.performed += _ => PlayerAimingPerformed();
    }
        private void PlayerAimingStart()
        {
            shootingPositionStart = controls.Gameplay.MousePosition.ReadValue<Vector2>(); shootingPositionStart.z = 0f;
        }
    
        private void PlayerAimingPerformed()
        {
            shootingPositionEnd = controls.Gameplay.MousePosition.ReadValue<Vector2>(); shootingPositionEnd.z = 0f;
            Vector3 force = shootingPositionStart - shootingPositionEnd;
            Vector3 clampedForce = Vector3.ClampMagnitude(force, maxDrag) * power;
            bulletGO.GetComponent<Rigidbody2D>().AddForce(clampedForce, ForceMode2D.Impulse);
        }
1 Answers

In the input action asset, under my gamplay action map and action, Gameplay > Actions: Shoot/Left Button

I had to add a press interaction on the right hand side Interactions > Press

and set the trigger behavior to Press And Release.

Related