camera orbit and zoom

Hi guys,

I’m new at coding and I’m trying to frankenstein together bits of code to make the camera orbit around an object when dragged/swiped and move the camera forward/backwards by pinching/scrolling.

I’m not completely succesful, the zooming only goes into effect if I orbit…
I would also like to be able to move the target and have the distanceToTarget updated… any help is appreciated.

Kindest regards,
Filip

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

public class CameraMovement : MonoBehaviour
{
    [SerializeField] private Camera cam;
    [SerializeField] private Transform target;
    [SerializeField] [Range(0, 360)] private int maxRotationInOneSwipe = 180;
     float MouseZoomSpeed = 15.0f;
     float TouchZoomSpeed = 0.1f;
     float distanceToTarget = 10;
 
    // Use this for initialization
 
 
    private Vector3 previousPosition;
 
    void Update()
    {
                if (Input.touchSupported)//pinch code
        {
            // Pinch to zoom
            if (Input.touchCount == 2)
            {

                // get current touch positions
                Touch tZero = Input.GetTouch(0);
                Touch tOne = Input.GetTouch(1);
                // get touch position from the previous frame
                Vector2 tZeroPrevious = tZero.position - tZero.deltaPosition;
                Vector2 tOnePrevious = tOne.position - tOne.deltaPosition;

                float oldTouchDistance = Vector2.Distance (tZeroPrevious, tOnePrevious);
                float currentTouchDistance = Vector2.Distance (tZero.position, tOne.position);

                // get offset value
                float deltaDistance = oldTouchDistance - currentTouchDistance;
                Zoom (deltaDistance, TouchZoomSpeed);
            }
        }
        else
        {

            float scroll = Input.GetAxis("Mouse ScrollWheel");
            Zoom(scroll, MouseZoomSpeed);
         
         
         
         
            //end pinch code
        }
        if (Input.GetMouseButtonDown(0))
        {
            previousPosition = cam.ScreenToViewportPoint(Input.mousePosition);
        }
        else if (Input.GetMouseButton(0))
        {
            Vector3 newPosition = cam.ScreenToViewportPoint(Input.mousePosition);
            Vector3 direction = previousPosition - newPosition;
         
            float rotationAroundYAxis = -direction.x * maxRotationInOneSwipe; // camera moves horizontally
            float rotationAroundXAxis = direction.y * maxRotationInOneSwipe; // camera moves vertically
         
            cam.transform.position = target.position;
         
            cam.transform.Rotate(new Vector3(1, 0, 0), rotationAroundXAxis);
            cam.transform.Rotate(new Vector3(0, 1, 0), rotationAroundYAxis, Space.World); // <— This is what makes it work!
         
            cam.transform.Translate(new Vector3(0, 0, -distanceToTarget));
         
            previousPosition = newPosition;
        }
    }
        void Zoom(float deltaMagnitudeDiff, float speed)//test
        {

        distanceToTarget += deltaMagnitudeDiff * speed;
        // set min and max value of Clamp function upon your requirement
        distanceToTarget = Mathf.Clamp(distanceToTarget, 1, 100);
    }
}

//trying to merge code from following sites to make the camera rotate around a target object by swiping/dragging and zooming by pinching/scrolling to adjust the "distanceToTarget"
//https://stackoverflow.com/questions/59030399/zooming-in-unity-mobile
//https://emmaprats.com/p/how-to-rotate-the-camera-around-an-object-in-unity3d/

In your Zoom function, just add

transform.position = target.position;
transform.Translate(new Vector3(0, 0, -distanceToTarget));

just after you clamp the distanceToTarget variable.