Hello,
I’m working on a small game that has a perspective camera but looks straight down Z for the most part. I’ve made a small camera panning script that works functionally but I’ve found a couple of issues I’d like to understand more and address:
-
At higher pan speeds, the camera starts shaking / juddering more. panSpeed is 40f by default and it works okay but you can definitely notice it. Putting it up to 60 or 70 can cause the camera to fly off.
-
I find the position of the mouse when the left button is held down and find the difference in distance as it moves. I apply this offset to the camera to move it. If I swap these around, the camera movement is inversed but the juddering at higher speeds seems to be gone. You can see this when you swap the comments on :
//Vector3 newOffset = origPanPos - oldPanPos; Vector3 newOffset = oldPanPos - origPanPos;
Can anyone help my understand the issue?
Thanks
using UnityEngine;
using System.Collections;
public class CameraPanAndZoom : MonoBehaviour {
public GameObject targetDepth;
private float screenDepth;
private Vector3 origPanPos;
private bool isPanning;
private Vector3 oldPanPos;
public float panSpeed = 40f;
void Start(){
screenDepth = Mathf.Abs(transform.position.z - targetDepth.transform.position.z);
}
void Update(){
if(Input.GetButtonDown("Fire1")){;
oldPanPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenDepth));
isPanning = true;
}
if (isPanning){
origPanPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenDepth));
//Swapping origPanPos and oldPanPos inverses the camera control but gets rid of the judder
//Vector3 newOffset = origPanPos - oldPanPos;
Vector3 newOffset = oldPanPos - origPanPos;
transform.Translate(newOffset * panSpeed * Time.deltaTime, Space.Self);
oldPanPos = origPanPos;
}
if (Input.GetMouseButtonUp(0)){
isPanning = false;
}
}
}