How can ı rotate camera with touch?

Hi everyone ı want to rotate camera with touch.

ı tried this logic:

Function Update()
{
İf(Input.GetTouch(0).phase==TouchPhase.Began)
{
 var firstposint:Transform;
 firstpoint.position=Input.GetTouch(0).position;
 

}
if(Input.GetTouch(0).phase==TouchPhase.Stationy)
{
  var seconpoint:Transform;
  seconpoint.position=Input.GetTouch(0).position;
  this.camera.transform.rotation.x += seconpoint.position.x - firstpoint.position.x;
  this.camera.transform.rotation.y += seconpoint.position.y - firstpoint.position.y;
}
}

this isnt work! ı dont understand ı am getting to first touch position for example firstpoint=20 and while ı was touching to screen ı am getting position of second touch for example seconpoint=40

and ı am setting to them differences to rotation of camera!
where ı am wrong? pls help me!

Method transform.rotation is Quaternion. For more information about quaternion see docs. I correct your code:

 private var firstpoint:Vector3; //change type on Vector3
 private var secondpoint:Vector3;

 private var xAngle: float = 0.0; //angle for axes x for rotation
 private var yAngle: float = 0.0;
 private var xAngTemp: float = 0.0; //temp variable for angle
 private var yAngTemp: float = 0.0;

 function Start() {
  //Initialization our angles of camera
  xAngle = 0.0;
  yAngle = 0.0;
  this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0);
 }

 function Update() {
  //Check count touches
  if(Input.touchCount > 0) {
   //Touch began, save position
   if(Input.GetTouch(0).phase == TouchPhase.Began) {
    firstpoint = Input.GetTouch(0).position;
    xAngTemp = xAngle;
    yAngTemp = yAngle;
   }
   //Move finger by screen
   if(Input.GetTouch(0).phase==TouchPhase.Moved) {
    secondpoint = Input.GetTouch(0).position;
    //Mainly, about rotate camera. For example, for Screen.width rotate on 180 degree
    xAngle = xAngTemp + (secondpoint.x - firstpoint.x) * 180.0 / Screen.width;
    yAngle = yAngTemp - (secondpoint.y - firstpoint.y) *90.0 / Screen.height;
    //Rotate camera
    this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0);
   }
  }
 }

Attach this script at Main Camera on your scene. Of course, you may to use Touch.deltaPosition, but I write common method. I hope, that it will help you.

This code really helped me a lot! I tried a few different things and this turned out to be the right answer for my project. I did have one issue. When I rotated my camera on the y-axis until it was upside down, the x-axis was reversed. Here is my update to solve the issue. I am also clamping the values to be 0 to 360. My code is also for rotating the Player, not a camera, but that can be easily changed.

My project is written in c#. @zharik86

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


public class touchCam : MonoBehaviour {

	private Vector3 firstpoint; 
	private Vector3 secondpoint;
	private float xAngle = 0.0f; //angle for axes x for rotation
	private float yAngle = 0.0f;
	private float xAngTemp = 0.0f; //temp variable for angle
	private float yAngTemp = 0.0f;
	private GameObject playRot;


	void  Start() {
		
		dtext=GameObject.Find("debugText").GetComponent<Text>();

		firstpoint = new Vector3 (0, 0, 0);
		secondpoint = new Vector3 (0, 0, 0);

		playRot = GameObject.Find ("Player");
	
		xAngle = 0.0f; 
		yAngle = 0.0f;
   
		transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
	}

	void Update() {
		//Check count touches
		if (PlayerController.instance.TabletMode == 1) {
			if (Input.touchCount > 0) {
				//Touch began, save position
				if (Input.GetTouch (0).phase == TouchPhase.Began) {
					firstpoint = Input.GetTouch (0).position;
					xAngTemp = xAngle;
					yAngTemp = yAngle;
				}
				//Move finger by screen
				if (Input.GetTouch (0).phase == TouchPhase.Moved) {
					secondpoint = Input.GetTouch (0).position;
					//Mainly, about rotate camera. For example, for Screen.width rotate on 180 degree
					yAngle = yAngTemp - (secondpoint.y - firstpoint.y) * 90.0f / Screen.height;

					if (yAngle < 0)
						yAngle  +=360;
					if (yAngle > 360)
						yAngle  -=360;

					if (yAngle > 90 && yAngle < 270)
						xAngle = xAngTemp - (secondpoint.x - firstpoint.x) * 180.0f / Screen.width;
					else
						xAngle = xAngTemp + (secondpoint.x - firstpoint.x) * 180.0f / Screen.width;

					if (xAngle < 0)
						xAngle  +=360;

					if (xAngle > 360)
						xAngle  -=360;
					
					transform.rotation = Quaternion.Euler (yAngle, xAngle, 0.0f);

				}
			}
		}
	}
}

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

public class rotate : MonoBehaviour
{

float rotSpeed =20;

void OnMouseDrag ()
{
	float rotX = Input.GetAxis("Mouse X")*rotSpeed*Mathf.DegZRad;
	float rotY = Input.GetAxis("Mouse Y")*rotSpeed*Mathf.DegZRad;

	transform.RotateAround(Vector3.up, -rotX);
	transform.RotateAround(Vector3.right, rotY);
}

}

{
Vector3 FirstPoint;
Vector3 SecondPoint;
float xAngle;
float yAngle;
float xAngleTemp;
float yAngleTemp;
public float ymax;
public float ymin;

    void Start()
    {
       

        xAngle = 0;
        yAngle = 0;
        

    }

    void Update()
    {
       
        if (Input.touchCount > 0)
        {
            bool yclamp = false;
            if (Input.GetTouch(0).phase == TouchPhase.Began)
            {
                FirstPoint = Input.GetTouch(0).position;
                xAngleTemp = xAngle;
                if(!yclamp)
                yAngleTemp = yAngle;
            }
            if (yAngle > ymax && yAngle - yAngleTemp > 0 || yAngle < ymin && yAngle - yAngleTemp < 0)
            {
                yclamp = true;

                if (Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                    SecondPoint = Input.GetTouch(0).position;
                    xAngle = xAngleTemp + (SecondPoint.x - FirstPoint.x) * 180 / Screen.width;
                    // yAngle = yAngleTemp + (SecondPoint.y - FirstPoint.y) * 90 / Screen.height;
                    this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
                }
            }
            else
            {
                if (Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                    SecondPoint = Input.GetTouch(0).position;
                    xAngle = xAngleTemp + (SecondPoint.x - FirstPoint.x) * 180 / Screen.width;
                    yAngle = yAngleTemp + (SecondPoint.y - FirstPoint.y) * 90 / Screen.height;
                    this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
                    yclamp = false;
                }


                Debug.Log(yAngle);
            }
        }

    }
   
}

@Jaskirat_Games this works smoothly for me to clamp y axis

The above code nearly solved my problem but could anybody help me to clamp the rotation in x and y axis.@zharik86 .

using UnityEngine;
public class CameraRotation : MonoBehaviour
{
public float sensitivity;
public Transform body;
Vector3 firstPoint, secondPoint;
float xRotation, yRotation;
float tempXrotation, tempYrotation;
void Update ()
{
TouchControll();
Rotation();
}
public void TouchControll ()
{
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
firstPoint = Input.GetTouch(0).position;
tempYrotation = yRotation;
tempXrotation = xRotation;
}
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
secondPoint = Input.GetTouch(0).position;
yRotation = (tempYrotation + (secondPoint.x - firstPoint.x)sensitivity/Screen.width);
xRotation = (tempXrotation + (secondPoint.y - firstPoint.y) * sensitivity / Screen.width);
}
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
tempYrotation = yRotation;
tempXrotation = xRotation;
}
}
}
public void Rotation ()
{
//xAngle = Mathf.Clamp(xAngle, -90f, 90f);
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
body.transform.rotation = Quaternion.Euler(Vector3.up
yRotation) ;
transform.localRotation = Quaternion.Euler(-xRotation, 0f, 0f);
}
}

Here is my realization of Camera Rotation by swipe (touch). It written with new Input System support. As my project is a car game, I need only X axis. But You can easly add Y axis too.
Also this code has a filter to prevent conflicts with UI elements.

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem.EnhancedTouch;
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
using TouchPhase = UnityEngine.InputSystem.TouchPhase;

public class SwipeController : MonoBehaviour
{
    private Touch activeTouch;
    private Vector3 FirstPoint;
    private Vector3 SecondPoint;

    public float swipeSpeed = 0.2f;

    public static float swipeDirX = 0;
    public static Vector3 xSwipeAmount;


    private void Start()
    {
        EnhancedTouchSupport.Enable();
        xSwipeAmount = Vector3.zero;
    }



    private bool IsPointerOverUIObject(Vector2 pos)
    {
        PointerEventData eventDataCurrentPosition = new(EventSystem.current);
        eventDataCurrentPosition.position = pos;
        List<RaycastResult> results = new();
        EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
        return results.Count > 0;
    }


    private void Update()
    {
        if (Touch.activeTouches.Count > 0)
        {
            activeTouch = Touch.activeTouches[0];

            if (IsPointerOverUIObject(activeTouch.screenPosition)) return;

            if (activeTouch.phase == TouchPhase.Began)
            {
                FirstPoint = activeTouch.screenPosition;
                swipeDirX = 0;
            }
            else if (activeTouch.phase == TouchPhase.Moved){
                SecondPoint = activeTouch.screenPosition;
                swipeDirX = SecondPoint.x - FirstPoint.x;
            }
        }
        else
        {
            swipeDirX = 0;
        }

        xSwipeAmount = swipeDirX * swipeSpeed * Time.deltaTime * Vector3.up;
    }

}