Drag camera script does not work propertly for Orthographic

Hi guys,

first post. Pretty new to Unity and I’m noticing that this script I attached to the main Camera object does not work as I want it to. My game camera is in Ortho, and I want the scene / camera to scroll straight down if I drag straight down. It instead scrolls diagonally to the lower right corner when I do that. I think it’s because I’m in Ortho mode and scrolling diagonally to the lower right corner is considered scrolling “straight down” from the point of rotation of the camera…but that’s not how I want it to work. Is there a way to fix it? Basically I want to calculate the move as I am, but then rotate the move by 45 degrees clockwise I think before applying it. Not sure what kind of math is used to do that kind of thing.

Here’s my script:

public class CameraScript : MonoBehaviour {
private Vector3 startDragPosition;
int dragScrollSpeed = 450;

// Update is called once per frame
void Update () {
// drag scroll
if (Input.GetMouseButtonDown(1)) {
this.startDragPosition = Input.mousePosition;
return;
}

if (Input.GetMouseButton(1)) {
DragScrollCamera();
}
}

private void DragScrollCamera() {
float amtToMove = dragScrollSpeed * Time.deltaTime;

Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - this.startDragPosition);
Vector3 move = new Vector3(pos.x * amtToMove, 0, pos.y * amtToMove);

// scroll in the direction of the drag.
this.gameObject.transform.Translate(move, Space.World);
}
}

You’re converting from screen to viewport space, which changes each time you move the camera. This results in a cumulative effect that you probably do not want. Try moving the camera using the mouse position delta in screen space, without the conversion.

Ok, I did that. Still have the same problem as I originally asked though. Here’s the updated script:

public class CameraScript : MonoBehaviour {
private Vector3 startDragPosition;
float dragScrollSpeed = .6f;

// Update is called once per frame
void Update () {
// drag scroll
if (Input.GetMouseButtonDown(1)) {
this.startDragPosition = Input.mousePosition;
return;
}

if (Input.GetMouseButton(1)) {
DragScrollCamera();
}
}

private void DragScrollCamera() {
float amtToMove = dragScrollSpeed * Time.deltaTime;

Vector3 pos = Input.mousePosition - this.startDragPosition;
Vector3 move = new Vector3(pos.x * amtToMove, 0, pos.y * amtToMove);

// scroll in the direction of the drag.
this.gameObject.transform.Translate(move, Space.World);
}
}