- Home /
Input System - Detect if key for action was pressed or released
I have a simple setup with one Map and one Action, lets say "Jump" with a binding to "Space".
Now I want to start a charge up animation on "Press" and do the actual jump on "Release" so I a trigger behavior Press And Release
. I added an Subscriber to my Event but I can't find a way to determine if I am currently in the "Press" or in the "Release" state.
Or should I add two Actions? "Charge" and "Jump". One with the "Press Only" behavior and the other with "Release Only"
Can you show some code? Like I have no idea what you are talking about, which event? Are you using some kind of asset?
He is talking about the new Input System. And that event is probably provided through the PlayerInput component with an Input Actions asset assigned.
I agree with you, he should show some code. The new Input System is fairly complex, with multiple ways of achieving the same thing. It would be very hard to help him without taking a look at the code.
Here you can see the Input Actions with "Press and Release" and the Component
And from my Player-Script
public void Shoot(InputAction.CallbackContext context)
{
Debug.Log(context.valueType + " - " + context.action + " - " + context.control + " - " +
context.started + " - " + context.performed + " - " + context.canceled + " - phase: " +
context.phase + " - " + context.interaction);
}
public void $$anonymous$$ove(InputAction.CallbackContext context)
{
Debug.Log("$$anonymous$$ove " + context.ReadValue<float>());
}
I use the new Unity Input system https://blogs.unity3d.com/2019/10/14/introducing-the-new-input-system/
If you don't show your code, we won't be able to help you
Answer by rekatha · Jul 09, 2020 at 06:54 AM
this is what you need to do when choose Press And Release
. First Add Player Input
component. And then set Player Input
behavior to Invoke Unity Event
. Add function from your script, let say OnJump
This is logic for press and release button:
//Callback for "Space" button
public void OnJump(InputAction.CallbackContext context)
{
if (context.started)
{
//button is press
}
else if (context.cancelled)
{
//button is released
}
}
@rekatha something change since you post this? In my case "context.canceled" is only called after I turn off the game (inside Unity). I'm using Press[Touchscreen] as binding, and when I lift my finger "context.canceled" isn't called (tested on my phone).
Answer by edthered1009 · Feb 13, 2020 at 01:43 AM
You can have a single action (for example "Jump"), and do this: if (GetButtonDown("Jump")) { //Charge } if (GetButtonUp("Jump")) { //Jump }
Hope this helps!
Your answer
Follow this Question
Related Questions
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
URGENT: Info request about run gyroscope with Input.acceleration Script in UNITY-3D !!! 0 Answers
New input system controller stops detecting on fully pressed trigger? 0 Answers
Detect if running on mobile vs desktop 2 Answers
Input System Can't Catch Event on Update 0 Answers