- Home /
Detect the end of input with the new input system
I'm trying to make a system with the new input system, so that the player character accelerates while he is being moved, and the speed resets when he stops moving. Currently my code does manage to both move the player and enable the acceleration, but it doesn't detect the end of the player moving the character and disable the acceleration. Help with how to detect the end of input with the new input system?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMover : MonoBehaviour
{
private PlayerControls playerControls;
public float moveSpeed = 50f;
public float maxSpeed = 100f;
public float acc = 10f;
public float baseSpeed = 50f;
private Rigidbody2D rb;
public bool accOn = false; //whether to accelerate movement or not
private void Awake()
{
playerControls = new PlayerControls();
rb = GetComponent<Rigidbody2D>();
playerControls.Gameplay.Move.performed += Accelerate; //player is moving character
playerControls.Gameplay.Move.canceled += Reset; //player stops moving character
}
private void OnEnable()
{
playerControls.Enable();
}
private void OnDisable()
{
playerControls.Disable();
}
void Accelerate(InputAction.CallbackContext context)
{
accOn = true;
}
void Reset(InputAction.CallbackContext context)
{
accOn = false;
}
void FixedUpdate()
{
Vector2 movementInput = playerControls.Gameplay.Move.ReadValue<Vector2>();
if (accOn==true)
{
moveSpeed += acc * Time.deltaTime;
}
else
{
moveSpeed = baseSpeed;
}
rb.velocity = movementInput * moveSpeed * Time.deltaTime;
}
}
Answer by ricky_lee_ · Sep 18, 2021 at 05:15 PM
The context variable contains information that you can use to determine if an input has been released, it also shows you when an input is initially pressed as well as if you are holding it down.
This context information can be seen as a bool in this way:
context.started // True when the button is initially pressed
context.performed // True while if you are holding the button down
context.canceled // True when the button gets released
Because I don't know exactly how your Accelerate() or Reset() are being triggered, I'm going to just assume that your Accelerate() is triggered by pressing 'W' or something.
So I've changed your Accelerate() to include an if() for all three of these contexts, just so you can see how and when they are true/false.
After playing with it hopefully you can see how to use this to fix your problem :)
void Accelerate(InputAction.CallbackContext context)
{
accON = true;
if (context.started)
{
Debug.Log("Initial button press");
}
if (context.performed)
{
Debug.Log("Button is being pressed");
}
if (context.canceled)
{
Debug.Log("Button has been released");
}
}
edit 1: Context contains a lot more than just the three pieces of information I've shown. The way I worded my answer is as if there are only these three pieces of information. All of them are listed here
edit 2:
Just to try and help you further, I'm guessing that your accON becomes true but then never sets to false?
If this is the case you would just need to do something like this, continuing off what I said before:
void Accelerate(InputAction.CallbackContext context)
{
if (context.started)
{
accON = true;
}
if (context.canceled)
{
accON = false;
}
}
Or for an even cleaner looking way to do the same thing, using context.performed instead:
void Accelerate(InputAction.CallbackContext context)
{
accON = context.performed;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613043157im_/https://answers.unity.com/themes/thub/images/avi.jpg)