Fire-Type action using Unity Events Input? (Unity Input System 1.0.2)

I am currently using “Invoke Unity Event” to get an input and I am having trouble turning that into a Fire-Type action. With my current code, I get both the press and hold, and I can’t figure out how to change that to just a press.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Shooting : MonoBehaviour
{
    private bool shootbool;
    private float intshoot;
    public Transform firepoint;
    public GameObject bulletPrefab;
    public float bulletForce = 200f;

    public void shoot(InputAction.CallbackContext shootValue){
        shootbool = shootValue.ReadValueAsButton();
    }

    private void shooted()
    {
        GameObject Projectile = Instantiate(bulletPrefab, firepoint.position, firepoint.rotation);
        Rigidbody2D rbp = Projectile.GetComponent<Rigidbody2D>();
        rbp.AddForce(firepoint.right * bulletForce, ForceMode2D.Impulse);
    }

    private void FixedUpdate()
    {
        if(shootbool == true)
        {
            shooted();
        }
    }
}
  1. may I see your Input Actions Asset Properties setup
  2. why you need hold? if press is meant to shoot and what hold is meant to do?
  3. are you press down to shoot or you release press to shoot?

6878648--803417--upload_2021-2-25_18-19-7.png


6878648--803420--upload_2021-2-25_18-19-31.png

  1. It is currently acting like a hold where the bool stays true as long as I hold the bottom down, but I want it to only activate on the press of the button

  2. I am pressing down to shoot

you don’t need press and hold interactions just remove both, it’s not necessary

public void Shoot(InputAction.CallbackContext shootValue)
        {
            switch(shootValue.phase)
            {
                case InputActionPhase.Started:
                    {
                        //Start Phase common use for reset properties or active something
                        shootbool = true;
                        Debug.Log("I'm start shooting");
                    }
                    break;
                case InputActionPhase.Performed:
                    {
                        //shootbool will stay true till Canceled is call

                        //this will deactivated shootbool while holding the button
                        //shootbool = false;


                        string text =  (shootbool) ? "I'm still shooting" : "I'm done shooting";
                        Debug.Log(text);
                    }
                    break;
                case InputActionPhase.Canceled:
                    {
                        string text = (shootbool) ? "I'm done shooting" : "I'm done shooting before you release the button";

                        //Reset properties or deactivated
                        shootbool = false;


                        Debug.Log(text);
                    }
                    break;
            }
        }
1 Like

Is there a way to initiate InputActionPhase.Started without a switch statement?

if(shootValue.started)
   {

    }