[SOLVED]
The issue was that while the event was triggering when pressing a movement button (e.g. W to move forward), I was updating the movement only there, when the event triggered. Said event would only trigger when I press and release the button, because it changed from (0.0, 1.0) to (0.0, 0.0), and that’s it.
So the solution was to cache the (normalized) Vector2 received from the event invocation and with that constantly update the movement in “Update()”.
So yeah, pretty newbie mistake.
[THE PROBLEM]
Hi all! I’m learning how to use the Input System together with Scriptable Objects and I’m having a little problem.
I created a InputReaderSO (Scriptable Object) which will be responsable for handling inputs and casting to all listeners which are subscribed to the corresponding input event, in this case, the PlayerMovementController.
I have 4 files:
- GameInputs (Input Actions Scheme).
- GameInputs.cs (c# file generated from the Input Actions).
- InputReaderSO (Scriptable Object)
- PlayerMovementController
The Input Actions generated file is needed by the InputReaderSO in order to access the Actions Callbacks (to know when an action is being performed sort of speak)
Here is an example on how the Input Reader is handling the Inputs and event invocations
public void OnMove(InputAction.CallbackContext context)
{
if (moveEvent != null)
{
moveEvent.Invoke((context.ReadValue<Vector2>()));
}
}
public void OnLook(InputAction.CallbackContext context)
{
if (lookEvent != null)
{
lookEvent.Invoke((context.ReadValue<Vector2>()));
}
}
I am subscribing and unsubscribing from the input reader events in the character controller in OnEnable and OnDisable.
Now, this is how the Character controller is processing the data:
private void OnMove(Vector2 moveInput)
{
float baseSpeed = _isSprinting ? _sprintSpeed : _baseSpeed;
Vector3 input = new Vector3(moveInput.x, 0, moveInput.y);
transform.Translate(Time.deltaTime * baseSpeed * input);
}
private void OnLook(Vector2 lookInput)
{
Vector3 rotation = _cameraSensitivity * 0.01f * new Vector3(lookInput.x, lookInput.y, 0);
_xRotation += rotation.y;
_xRotation = Mathf.Clamp(_xRotation, -_cameraClampDegrees, _cameraClampDegrees);
_cameraTransform.localRotation = Quaternion.Euler(_xRotation, 0, 0);
transform.Rotate(rotation.x * Vector3.up);
}
My issue is that while the camera movement is responding right, the character movement “OnMove” is only being processed once after holding a movement button (e.g. W to move forward). You can see the Input Actions Maps in the attached Image. My apologies, but I don’t know how to embed a picture in the post it self.
To recap:
- The OnLook event (mouse input or right stick Gamepad) is sending data continuously, but the OnMove event isn’t, it is being registered only once, I have to tap multiple times in order to move instead of holding the key to move.
Thank you so much beforehand.
