Moving a player object using the input system events rather than the Update method

Hi,

I’m having fairly good success with the new Input System but my initial implementation was simply to create an input manager to capture the events and use the ouputs similar to the old input method. Having spent a bit more time on this I have tried to change my code to get some of the event driven benefits of this new system.

Currently I am moving a character using the following

    void OnMovePerformed(InputAction.CallbackContext obj) {
        move = obj.ReadValue<Vector2>().normalized;
        rb2d.velocity = move * speed;

    }

    void OnMoveReleased(InputAction.CallbackContext obj) {
        move = Vector2.zero;
        rb2d.velocity = move * speed;
    }

By doing this I don’t need to set the character’s rigidbody velocity in the Update method but is this going to catch me out at some point later in terms of performance?

I don’t think it’ll slow down your performance too much. You also could have your movement script in Update() and change all other variables through events (thats how I’d manage that with the new input system).

Here’s example how to do that:

public float jumpPower = 20;
float jumpMultiplier = 1;
public float speed = 5;
Vector2 dir = Vector2.zero;
bool canMoveJump = true;

void Start()
{
    jumpAction.performed += (ctx) => { Jump(); };
    moveAction.performed += (ctx) => { dir = ctx.ReadValue<Vector2>().normalized; };
    moveAction.cancelled += (ctx) => { dir = Vector2.zero; };
    openMenuAction += (ctx) => { canMoveJump = ctx.ReadValue<bool>(); };
}

void Update() => rb2d.velocity = dir * speed * time.deltaTime * (canMoveJump ? 1 : 0);
public void Jump() => rb2d.AddForce(jumpPower  *  jumpMultiplier  * Time.deltaTime * (canMoveJump ? 1 : 0)); 
}