I cant move camera in all directions

it only scrolls left, i cant get it to scroll right until I scroll it to the left. When trying to scoll up, it jumps up a scree instead of scrolling. Cant scroll down until after the screen jumped up but it doesnt scroll down it jumps down too. The only scrolling is left and right.

Would be nice if up and down scrolling will work too.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraMove : MonoBehaviour
{

    public float panSpeed = 10f;  //how fast the screen moves when mouse is at an edge
    public float panBorderThickness = 10f;
    public Vector2 panLimit;

  

    float inputX, inputZ;


    void Update()  //the mouse works in this section, and only the left and right keys works
                  //put cursor at border of the map to scroll the screen
    {
        Vector3 pos = transform.position;


        { if (Input.GetKey("w") || Input.mousePosition.y >= Screen.height - panBorderThickness)
                pos.z += panSpeed * Time.deltaTime;
        }


        {
            if (Input.GetKey("s") || Input.mousePosition.y <= panBorderThickness)
                pos.z -= panSpeed * Time.deltaTime;
        }

   
     {if (Input.GetKey("d") || Input.mousePosition.x >= Screen.width - panBorderThickness)  //moves screen right
                pos.x += panSpeed* Time.deltaTime;
}


    {
       if (Input.GetKey("a") || Input.mousePosition.x <= panBorderThickness) //moves left with the key...
                                                                             //...and when mouse cursor is at the left edge
                pos.x -= panSpeed* Time.deltaTime;
}




//these next two lines stops the screen from moving when reaching the border...
        //...limit.  Set that in the inspector.
        //for some reason, its starting my screen away form where I want it at the fitst limit
        pos.x = Mathf.Clamp(pos.x, -panLimit.x, panLimit.x);
        pos.z = Mathf.Clamp(pos.z, -panLimit.y, panLimit.y);


        transform.position = pos;


    }
}

You are going to confuse yourself very quick, with that messy bracketing approach.

I suggest you use Debug.Log, to track bugs.

1 Like

By pure chance, switching the camera to “perspective” actually found the problem. “w” and “s” actually zooms it in and out respectively. in both projections, “a” and “d” works properly.

But why does “w” and “s” zooms in and out when clearly the code states it should scroll screen up or down?

As for W and S; Z is forward vector.
You probably want to change to Y vertical vector.

1 Like

That worked, thanks! Who knew it was something so simple as just changing one letter (well two total for one letter in both lines of code) haha.

I guess you cant copy verbatim the code that works for a 3d game into a 2d game.

Guess I forgot some geometry from high school haha.