I'm trying to set up two different ways of shooting using the same input.

So like the title suggests, I’m trying to make a script that has two different behaviors based on which gun I have in hand. I’m using the new Unity Input system and so far it’s served me pretty well, but this issue really has me stumped.

I have two different types of weapons, one’s a one shot type weapon, the other will be a rapid fire type weapon. With the one shot type weapons I want to fire only when the trigger is pressed (so using the start phase). And for the rapid fire weapons the trigger will have to be held (so using the performed phase).
I’m pretty sure that’s what I have my code doing right now, but bringing the results over to the class that’ll be using it everything seems to be every update. Even though I have checks in place to have it only happen when it’s supposed to.

I guess what I’m really asking is if someone could look over my code and tell me what I’m doing wrong :frowning:

This is my inputHandler class that I’m getting input from:

using AL;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices.ComTypes;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;

public class InputHandler : MonoBehaviour
{
    public float horizontal;            //Our horizontal input.
    public float vertical;              //Our vertical input.
    public float moveAmount;            //How much we're moving in any direction.
    public float mouseX;                //The movement of our mouses X-axis.
    public float mouseY;                //The movement of our mouses Y-axis.

    public PlayerLocomotion inputActions;      //A reference to our Input Actions.
    Vector2 moveInput;                  //A globle variable to store our move input in (x, y) (Also because these values are vector2s).
    Vector2 cameraInput;                //A globle variable to store our camera input in (x, y).
    GunManager gunManager;

    public bool rightTriggerHeld;
    public bool rightTriggerPressed;
    public bool leftTrigger;
    public bool pauseButton;

    //Called once the object becomes enabled.
    public void OnEnable()
    {
        if (inputActions == null)
        {
            inputActions = new PlayerLocomotion();
            inputActions.PlayerMovement.Movement.performed += ctx => moveInput = ctx.ReadValue<Vector2>();
            inputActions.PlayerMovement.Movement.canceled += ctx => moveInput = Vector2.zero;
            inputActions.PlayerMovement.CameraMovement.performed += ctx => cameraInput = ctx.ReadValue<Vector2>();

            inputActions.PlayerActions.Shooting.started += ctx => R2Pressed(ctx);
            //inputActions.PlayerActions.Shooting.canceled += ctx => rightTriggerPressed = false;
            inputActions.PlayerActions.Shooting.performed += ctx => R2Held(ctx);
            inputActions.PlayerActions.Shooting.canceled += ctx => rightTriggerHeld = false;
            inputActions.PlayerActions.Zoom.performed += ctx => L2Pressed(ctx);
            inputActions.PlayerActions.Zoom.canceled += ctx => leftTrigger = false;

            inputActions.MenuActions.PauseGame.performed += ctx => PauseGame(ctx);
            inputActions.MenuActions.PauseGame.canceled += ctx => pauseButton = false;

            inputActions.Enable();
        }
    }


    public void OnDisable()
    {
        inputActions.Disable();
    }

    public void TickInput(float delta)
    {
        MoveInput(delta);
    }

    public void MoveInput(float delta)
    {
        horizontal = moveInput.x;
        vertical = moveInput.y;
        moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
        mouseX = cameraInput.x;
        mouseY = cameraInput.y;
    }

    private bool L2Pressed(InputAction.CallbackContext context)
    {
        leftTrigger = context.performed;
        return leftTrigger;
    }

    private bool R2Held(InputAction.CallbackContext context)
    {
        if (context.phase == InputActionPhase.Performed)
        {
            Debug.Log("Trigger Held!");

            rightTriggerHeld = true;
        }

        return rightTriggerHeld;
    }

    private bool R2Pressed(InputAction.CallbackContext context)
    {
        if (context.phase == InputActionPhase.Started)
        {
            Debug.Log("Trigger Pressed!");

            rightTriggerPressed = true;
        }

        return rightTriggerPressed;
    }

    private bool PauseGame(InputAction.CallbackContext context)
    {
        pauseButton = context.performed;
        return pauseButton;
    }
}

And this is the class that I want the results of the R2Pressed() and R2Held() functions to be used in:

using System.Collections;
using UnityEngine;

namespace AL
{
    public class GunManager : MonoBehaviour
    {
        [Header("References")]
        public InputHandler inputHandler;
        public GameObject bullet;
        public Transform gunTip;
        CameraHandler camHandler;
        GunStats gunStats;
        Animator anim;

        bool isOneShot = false;
        bool hasShot = false;
        bool isRapidFireShot = false;

        public float timeHeldDown;

        private void Start()
        {
            inputHandler = GetComponentInParent<InputHandler>();
            camHandler = Camera.main.GetComponentInParent<CameraHandler>();
            gunStats = GetComponent<GunStats>();
            anim = GetComponentInParent<Animator>();


            if (gunStats.gameObject.layer == 12)
            {
                isOneShot = true;
                isRapidFireShot = false;
            }
            else if (gunStats.gameObject.layer == 13)
            {
                isOneShot = false;
                isRapidFireShot = true;
            }
        }

        private void Update()
        {
            float delta = Time.deltaTime;
            hasShot = inputHandler.rightTriggerPressed;

            Debug.Log(inputHandler.rightTriggerPressed);


            if (inputHandler.leftTrigger)
            {
                ZoomIn();

                HandleGunShooting(delta);

                if (timeHeldDown < 0f)
                {
                    timeHeldDown = 0f;
                }
            }
            else
            {
                anim.SetBool("IsAiming", inputHandler.leftTrigger);

            }
        }


        void HandleGunShooting(float delta)
        {
            if (isOneShot)
            {
                if (hasShot)
                {
                    Shooting(delta, true);
                }
            }
            else if (isRapidFireShot)
            {
                if (inputHandler.rightTriggerHeld)
                {
                    Shooting(delta);
                }
                else
                {
                    timeHeldDown -= delta;
                }
            }
        }

        void Shooting(float delta)
        {

            Debug.Log("Normal Shooting!");

            timeHeldDown -= delta;
            if (timeHeldDown <= 0)
            {
                Instantiate(bullet, gunTip);
                timeHeldDown = gunStats.fireRate;
            }

            gunTip.DetachChildren();
        }

        public void Shooting(float delta, bool foo)
        {
            Instantiate(bullet, gunTip);
            timeHeldDown = gunStats.fireRate;

            hasShot = false;
            gunTip.DetachChildren();
        }

        void ZoomIn()
        {
            anim.SetBool("IsAiming", inputHandler.leftTrigger);
        }

    }
}

I’m not sure if I need to refactor things completely or if I’m close to getting something that works.
Please anyone throw your input my way! I feel like just getting other people looking at this will help loads!
I also attached the files themselves incase anyone wants to look at them themselves! Thanks

6087759–660999–GunManager.cs (2.9 KB)
6087759–661002–InputHandler.cs (3.58 KB)

I would just have two more classes, one each for whatever type of gun you implement.

That way when you get to flame throwers it’s just another gun.

Also, please don’t add silly polls to your posts. It does not measurably add any value to anything here.

Thanks! I had a feeling that I would probably head in that direction!

And sorry, lol I just saw the feature and figured why not put it in!