#Hi, I have an issue regarding Input

1 messages · Page 1 of 1 (latest)

wooden spruce
fleet raft
#

Hi, I think so yes, however it becomes a bit too complicated for me. So my character object has a Character_Controller script on it and the Player Input component. Inside my Input Action Asset I have a button for Jump. In my Character_Controller script I want to be able to write something like CheckButton(Jump, pressed) and then trigger code based on it. How would I do this?

#

I have used chatgpt to try to write the input system and it works, but in play mode when I change the value of any variable inside the editor (like jumpStrength for example) it stops registering the buttons.

wooden spruce
#

To do something like that you should read closely the first link and the examples provided, this one

https://docs.unity3d.com/Packages/com.unity.inputsystem%401.5/manual/Actions.html#responding-to-actions

Scroll down and you should see examples like this, they should help 🙂

Example:

public PlayerInput playerInput;

public void Update()
{
    // IsPressed
    if (playerInput.actions["up"].IsPressed())
        transform.Translate(0, 10 * Time.deltaTime, 0);

    // WasPressedThisFrame
    if (playerInput.actions["teleport"].WasPressedThisFrame())
        Teleport();

    // WasReleasedThisFrame
    if (playerInput.actions["submit"].WasReleasedThisFrame())
        ConfirmSelection();
}
fleet raft
#

I see, so I tried to test it with this:

if (playerInput.actions["Jump"].IsPressed())
{
    Debug.Log("Jump PRESSED");
}
else if (playerInput.actions["Jump"].WasPressedThisFrame())
{
    Debug.Log("Jump HELD");
}
else if (playerInput.actions["Jump"].WasReleasedThisFrame())
{
    Debug.Log("Jump RELEASED");
}

}

Only Jump Released works as intended. Jump pressed is active when button is held, and jump held doesn't even register?

Edit: Ah I see, was supposed to write it like this:

if (playerInput.actions["Jump"].WasPerformedThisFrame())
{
Debug.Log("Jump PRESSED");
}
else if (playerInput.actions["Jump"].IsPressed())
{
Debug.Log("Jump HELD");
}
else if (playerInput.actions["Jump"].WasReleasedThisFrame())
{
Debug.Log("Jump RELEASED");
}

Thank you for your help, I really appreciate it!

wooden spruce
#

No problem! 😄

fleet raft
#

Can I ask you one more thing please? Is it bad to set the Input System Update Mode to "Process Events in Fixed Update" instead of Dynamic Update? I am testing everything now and it seems to work great, I don't notice any input lag. If I set it to Dynamic Update then my character movement and animations don't work with the buttons because my finite state machine is in FixedUpdate.

wooden spruce
#

Why is your state machine in fixed update, are you controlling physics object with inputs?

If that's the case then it's probably better to set the input update mode to be in fixed update because then your inputs will be processed at the same rate as your physics stuff

fleet raft
#

Yeah I have certain states like for example climbing that need to disable gravity and linearVelocityX or Y depending on the climb directon, and stuff like that

#

It's a 2D game, I apply velocity to the character's rigidbody2D

#

But then again I am worried that there might be issues or input lag due to processing inputs in FixedUpdate(), even though I don't notice it.

wooden spruce
#

There shouldn't be noticeable lag because of fixedupate, I think the default for fixedUpdate is 50 times per second, so you could have 50 changes of inputs in a single second which is such a small time scale it wouldn't be noticeable by any human really. Scripts could see the difference but then I think the only "risk" is that your script would be using a stale input, e.g. if you have a script and the update it running at 150fps then it'll have ~2/3 update cycles where it is using the input from the previous fixedUpdate

Should read up a little more about Update and FixedUpdate so you get a better understanding of what they are and their purpose, it'll be a big help with this and in the long run also 🙂