I am making a 2D game using Unity, and in the game you see the world from the side like in the first Super Mario games. But instead of moving the camera by walking, I want to move the camera by dragging with the mouse cursor. So basically move the cursor to the right of the screen click and hold and then move the cursor to the middle should move the camera to the right, and then vice versa to move left (So the camera is only moved on the x-axis). The game also has a feature to drag and drop objects and I used this tutorial for that: https://youtu.be/HfqRKy5oFDQ
And I am now trying to implement this by adding to the code from that tutorial with the help of this tutorial: https://youtu.be/Qd3hkKM-UTI. I have this code:
private Camera mainCamera;
private Transform camTransform;
private Vector3 origin;
private Vector3 difference;
private Vector3 resetCamera;
private void Awake()
{
mainCamera = Camera.main;
camTransform = mainCamera.transform;
}
private IEnumerator DragCameraUpdate()
{
while (mouseClick.ReadValue<float>() != 0)
{
//print("Mouse " + Time.deltaTime);
difference = (mainCamera.ScreenToWorldPoint(Mouse.current.position.ReadValue())) - camTransform.position;
//print("difference " + difference);
origin = mainCamera.ScreenToWorldPoint(Mouse.current.position.ReadValue());
//print(camTransform.position);
camTransform.position = origin - difference;
//print(camTransform.position);
yield return null;
}
}
So the loop itself works just as expected it loops through when the mouse is pressed and stopps when its not clicked anymore. The code to change the camera position, however, does not work. I tried printing out the position before and after it was supposed to change but it didn't change at all. I don't get any errors either. How can I fix this code so it moves the camera first of all and then also add code so it can only be moved on the x-axis?
Thanks in advance.