EDIT: I fixed it, but still like to know how the messages work.
I have a third person camera and want to move my character relative to the camera.
When the camera rotates, the character moves into the direction the camera looks.
After I replaced InputGetAxis
with Input Messages
the direction doesn’t change when rotating the camera. The character changes the direction only when I press another key or release it and press again.
It feels like the function is being called with the same “values” (old camera transform) until the InputValue changes.
What am I missing here?
I replaced this (called within Update):
private void HandleCharacterMovement()
{
Vector2 inputDirection = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Vector3 forward = _camera.transform.forward;
forward.y = 0f;
forward.Normalize();
Vector3 right = _camera.transform.right;
right.y = 0f;
right.Normalize();
Vector3 movementDirection = forward * inputDirection.y + right * inputDirection.x;
character.SetMovementDirection(movementDirection);
}
With this (called by input system message):
public void OnMove(InputValue value)
{
HandleCharacterMovement(value.Get<Vector2>());
}
private void HandleCharacterMovement(Vector2 inputDirection)
{
Vector3 forward = _camera.transform.forward;
forward.y = 0f;
forward.Normalize();
Vector3 right = _camera.transform.right;
right.y = 0f;
right.Normalize();
Vector3 movementDirection = forward * inputDirection.y + right * inputDirection.x;
character.SetMovementDirection(movementDirection);
}
EDIT: Fixed:
private Vector2 _inputDirection;
public void OnMove(InputValue value)
{
_inputDirection =value.Get<Vector2>();
}
private void HandleCharacterMovement()
{
Vector3 forward = _camera.transform.forward;
forward.y = 0f;
forward.Normalize();
Vector3 right = _camera.transform.right;
right.y = 0f;
right.Normalize();
Vector3 movementDirection = forward * _inputDirection.y + right * _inputDirection.x;
character.SetMovementDirection(movementDirection);
}