Hello, I’m making a 2D map in which I can zoom and move around it, I did that, but I’m not understanding 100% how the code works and I don’t know what I should modify to be able to change the speed with which I move by touching the screen and the speed with which I zoom (the zoom speed is very slow and the movement speed is very high). I leave the code, thanks.
public Camera cam;
private Vector3 touchStart;
public float maxZoom = 10;
public float minZoom = 100;
public float speed = 10;
float targetZoom;
void Start()
{
targetZoom = cam.orthographicSize;
}
void Update()
{
cameraController();
}
void zoom(float increment)
{
targetZoom = Mathf.Clamp(cam.orthographicSize - increment, maxZoom, minZoom);
cam.orthographicSize = Mathf.MoveTowards(cam.orthographicSize, targetZoom, speed * Time.deltaTime);
}
void cameraController() {
if (Input.GetMouseButtonDown(0))
{
touchStart = cam.ScreenToWorldPoint(Input.mousePosition);
}
if (Input.touchCount == 2)
{
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
float prevMagnitude = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float currentMagnitude = (touchZero.position - touchOne.position).magnitude;
float difference = currentMagnitude - prevMagnitude;
zoom(difference);
} else if (Input.GetMouseButton(0))
{
Vector3 direction = touchStart - cam.ScreenToWorldPoint(Input.mousePosition);
cam.transform.position += direction;
}
zoom(Input.GetAxis("Mouse ScrollWheel"));
}
}
PS: I added the Mathf.MoveTowards part because I thought that from that function I could change the speed but it didn’t work.