How to get the deactivate laser when using new input system OnFire method?

The question is given in the title,

Hi Naumaan,

What exactly do you mean? Whenever the OnFire method gets called, you could either enable or disable the laser. If you reference the laser game object, you could call laser.SetActive(true); or lasers.SetActive(false); on the laser game object. Enabling and disabling works the same way as in Rick’s code.

I hope this helped. :slight_smile:


See also:

That is pretty straight forward and understandable,
However when I use onFire method

It only activates the fire in that method, and I get no idea as to where to deactivate it ?

The new input system’s events have context. When you receive the event, you can check context.performed to determine when it was pressed and context.canceled to see when the button was released

1 Like

Thank you, that is very helpful, however i need to check how are they implemented to implement it myself.

The new input system has various ways of being used. How exactly are you using it? Knowing this will help me point you in the right direction

1 Like

I have made a new player input and then created a new asset pack from there, and then i directly started using OnMove function to get it working without any other addition.

Vector2 input;
    Rigidbody body;
    [SerializeField] float inputX;
    [SerializeField] float inputY;

void OnMove(InputValue value){
        input = value.Get<Vector2>();
    }
    void OnFire(InputValue value){
        int counter = 0;
        if(value.isPressed){
            // Debug.Log("fire pressed");
            Debug.Log(counter);
        }
    }

And this is how it is used in the inspector
Capture

Yeah, so I believe you can change OnFire(InputValue value) to OnFire(InputAction.CallbackContext context) and then you can use the context to check

void OnFire(InputAction.CallbackContext context)
{
    if (context.performed)
    {
        Debug.Log("Fire pressed");
    }
    else if (context.canceled)
    {
        Debug.Log("Fire released");
    }
}
1 Like

I tried this method already, but it gives this error

So I basically made it work by changing the action type of fire to value and its control type to integer
so whenever i press i get two values in on fire, 1 when the fire button is pressed and null when the fire button is released … for now it seems that it works … lets see,

bdw thanks for your quick responses, it may not helped but it means alot

1 Like

I don’t use this new input system in this way so I’m pretty unfamiliar with how it works so, I did a quick test; I had to change my action from Button to Value. I left everything else alone.
image
Now, the message is being sent twice; once when you press the button, and once when you release it. You can check value.isPressed to see if it is pressed or released;

void OnFire(InputValue value)
{
    if (value.isPressed)
    {
        Debug.Log("Fire pressed");
    }
    else
    {
        Debug.Log("Fire released");
    }
}
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms