1
\$\begingroup\$

I am testing out using the new Input System and cannot get the values from context.ReadValue<Vector2>() to pass into a private variable.

The simple setup is below:

using UnityEngine;
using UnityEngine.InputSystem;    

public class PlayerController : MonoBehaviour
{
    Vector2 leftThumbstickValues;
   
    void Start()
    {
        leftThumbstickValues = Vector2.zero;
    }

    void FixedUpdate()
    {
        Debug.Log("FixedUpdate: " + leftThumbstickValues); //Always displays "FixedUpdate: (0.0, 0.0)"
    }

    public void Move(InputAction.CallbackContext context)
    {
        leftThumbstickValues = context.ReadValue<Vector2>();
        Debug.Log("Move: " + leftThumbstickValues); //Displays correct values
    }
}

As you can see, in the Move function it correctly pulls the values from the thumbstick, but when it tries to use the values in FixedUpdate it always equals Vector2.zero. It seems like it should just work, but I think I must have overlooked something very simple.

Player Input Settings
Player Input Settings

Input Action Move Properties
Input Action Move Properties

Input Action Left Stick Properties
Input Action Left Stick Properties

EDIT: I decided to remove the Player Input component and subscribe to the events in code:

 controls.Player.Move.performed += cntxt =>
 {
     Move(cntxt);
 };
 controls.Player.Move.canceled += cntxt =>
 {
     leftThumbstickValues = Vector2.zero;
 };

This now works as expected. I'm not sure I should mark this as the answer though as it doesn't explain why it doesn't work with the original setup, just offers an alternative solution.

\$\endgroup\$
5
  • \$\begingroup\$ I don't think this answers your question, but in functions that handle a CallbackContext, you generally need to consider the phase so the action doesn't get triggered repeatedly. E.g add this to the top of the function: if (context.phase != InputActionPhase.Performed) return; \$\endgroup\$
    – Kevin
    Feb 26 at 21:19
  • 1
    \$\begingroup\$ When you include logging statements, it's helpful to share the exact console output, rather than just comments explaining the output next to the code. Seeing the console output can make it clearer when things are happening and the specific values that appear. \$\endgroup\$
    – Kevin
    Feb 26 at 21:22
  • \$\begingroup\$ Since the debug statement is generic to say "word + Vector2" I don't think you'd see anything more clear other than what OP said. It could confirm that the vector is indeed correctly read. The code provided looks the same as my code. The only difference is I didn't try a gamepad and I don't use PlayerInput anymore (because it subscribes events for all 3 phases) to set the callback function. \$\endgroup\$
    – D.Kallan
    Feb 28 at 4:51
  • \$\begingroup\$ @anothershrubery Perhaps the issue was related to input action phases, which I mentioned in my previous comment. Your alternative approach would not have the same issue with phases, because it only subscribes to specific phases. \$\endgroup\$
    – Kevin
    Mar 1 at 23:58
  • \$\begingroup\$ @D.Kallan Context is always important. It's not uncommon for someone to post a question where they quote a single line from the console output which doesn't give the full picture, time is wasted asking questions, then it turns out that their issue could easily have been diagnosed if they had shared more than 1 line of output. \$\endgroup\$
    – Kevin
    Mar 2 at 0:01
2
\$\begingroup\$

I had this exact same issue. The problem was that the Unity Editor got into a state where the Player Input component in the inspector was referring to a different instance of the Player than what was in the scene. This was easy to confirm by printing out the hashcode of the GameObject in both the input callback and in the FixedUpdate() block, which showed two different IDs!

This was fixed by re-assigning the Player Input callbacks in the Editor Inspector to the Player object in the scene. I have no idea what causes these to unsync in the first place. I hope this helps someone in the future.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.