0
\$\begingroup\$

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.

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

Let's do some algebra! You have three equations like this:

difference = mouseWorldPosition - oldCameraWorldPosition

origin = mouseWorldPosition

newCameraWorldPosition = origin - difference

We can substitute the definitions for origin and difference into the last equation and simplify:

newCameraWorldPosition = (mouseWorldPosition) - (mouseWorldPosition - oldCameraWorldPosition)
newCameraWorldPosition = mouseWorldPosition - mouseWorldPosition + oldCameraWorldPosition
newCameraWorldPosition = oldCameraWorldPosition

Congratulations. You've told the camera "stay where you were" with extra steps. 😜

You probably want something more like this:

Vector3 GetMousePositionInWorld() {
    Vector3 screenPosition = Mouse.current.position.ReadValue();

    // If you're using a perspective camera for parallax, 
    // be sure to assign a depth to this point.
    // screenPosition.z = 1f;

    return mainCamera.ScreenToWorldPoint(screenPosition);
);


private IEnumerator DragCameraUpdate()
{
    Vector3 initialMousePosition = GetMousePositionInWorld();

    while (mouseClick.ReadValue<float>() != 0)
    {
        Vector3 currentMousePosition = GetMousePositionInWorld();
        Vector3 travel = currentMousePosition - initialMousePosition;
        
        // Remove any vertical travel to lock the motion to the horizontal plane.
        travel.y = 0;

        mainCamera.transform.Translate(-travel, Space.World);

        yield return null;
    }
}
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .