I’ve been trying to switch my game over to using the new Input Action System instead of the old system but I’m running into an issue with a top down camera.
Before I could do something like this:
private void PanCamera()
{
var horizontal = Input.GetAxis("Horizontal");
var vertical = Input.GetAxis("Vertical");
if (horizontal == 0 && vertical == 0) return;
var xPosition = Camera.main.transform.position.x + (horizontal * panSpeed);
var yPosition = Camera.main.transform.position.y + (vertical * panSpeed);
Camera.main.transform.position = new Vector3(xPosition, yPosition, Camera.main.transform.position.z);
}
And this would allow me to move the camera in any direction, or diagonal if two buttons were held at once (W + A for example).
public PlayerInput PlayerInput;
private InputActionAsset _playerActions;
private float panSpeed = .5f;
private Vector2 movement = new Vector2();
private bool panCamera = false;
// Start is called before the first frame update
void Start()
{
_playerActions = PlayerInput.actions;
_playerActions.FindAction("MoveCamera", true).started += ctx => OnPanCameraStart(ctx);
_playerActions.FindAction("MoveCamera", true).canceled += ctx => OnPanCameraEnd(ctx);
}
// Update is called once per frame
void Update()
{
PanCamera();
}
void PanCamera()
{
if (!panCamera || movement == new Vector2()) return;
var xPosition = Camera.main.transform.position.x + (movement.x * panSpeed);
var yPosition = Camera.main.transform.position.y + (movement.y * panSpeed);
transform.position = new Vector3(xPosition, yPosition, transform.position.z);
}
void OnPanCameraStart(InputAction.CallbackContext context)
{
panCamera = true;
movement += context.ReadValue<Vector2>();
}
void OnPanCameraEnd(InputAction.CallbackContext context)
{
panCamera = false;
movement = new Vector2();
}
I’ve been trying to achieve the same effect using the new input system and this is where I last left off on. It will let me hold the button and scroll in one direction, but hitting another key does not cause the diagonal movement. And if I hit a second key + release the first key, it will just continue moving in the same direction. Which I guess makes sense from the fact that the Action is still continuing and has not been totally cancelled.
Does anyone have a better approach for this, or a different resource I can use as a reference? I believe I would need a way to continuously update the movement value as long as the action is ever running, rather than on started, but I’m not exactly sure how to do that.
Thanks!