Multiple touch input for Orbit

Hello,
Guys i created this script but don’t know how to make the rotation work on with 2 finger touch.
It only works with 1 finger touch. Need to make it work with 2 finger touch.Please help me to solve this problem.

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

    public class Rotate: MonoBehaviour {
    public GameObject Obj = null;
    public float minY = -45.0f;
    public float maxY = 45.0f;

    public float sensX = 20.0f;
    public float sensY = 20.0f;

    float rotationY = 0.0f;
    float rotationX = 0.0f;

        private float rotationRate = 0.8f;

        void Update () {
            foreach (Touch touch in Input.touches) {
                Debug.Log("Touching at: " + touch.position);

                if (touch.phase == TouchPhase.Began) {
                    Debug.Log("Touch phase began at: " + touch.position);
                } else if (touch.phase == TouchPhase.Moved) {
                    Debug.Log("Touch phase Moved");
                    Obj.transform.Rotate (touch.deltaPosition.y * rotationRate,
                        -touch.deltaPosition.x * rotationRate, 0, Space.World);
                } else if (touch.phase == TouchPhase.Ended) {
                    Debug.Log("Touch phase Ended");  
                }
            }
        }
    }

How exactly do you want it to work differently? It should only rotate when 2 fingers are moved, not with just 1?

Its only

Hello JoeStrout thanks for the reply. The thing is i have translate script that works with one finger and two finger . Same to rotate script too. So whenever i built this into mobile phone i have problem. I just want to make rotate object with 1 finger and translate object with 2 finger thats all. Please help me out JoeStrout

//This is the rotation script works with 1 touch and 2 touch

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

    public class NewBehaviourScript : MonoBehaviour {
    public GameObject Obj = null;
    public float minY = -45.0f;
    public float maxY = 45.0f;

    public float sensX = 20.0f;
    public float sensY = 20.0f;

    float rotationY = 0.0f;
    float rotationX = 0.0f;

        private float rotationRate = 0.8f;

        void Update () {
            // get the user touch inpun
            foreach (Touch touch in Input.touches) {
                Debug.Log("Touching at: " + touch.position);

                if (touch.phase == TouchPhase.Began) {
                    Debug.Log("Touch phase began at: " + touch.position);
                } else if (touch.phase == TouchPhase.Moved) {
                    Debug.Log("Touch phase Moved");
                    Obj.transform.Rotate (touch.deltaPosition.y * rotationRate,
                        -touch.deltaPosition.x * rotationRate, 0, Space.World);
                } else if (touch.phase == TouchPhase.Ended) {
                    Debug.Log("Touch phase Ended");  
                }
            }
        }
    }
//This is the translate script. It works with 1 touch and 2 touches also same to the rotation. 

using UnityEngine;

namespace Lean.Touch
{
    // This script allows you to transform the current GameObject with smoothing
    public class LeanTranslateSmooth : MonoBehaviour
    {
        [Tooltip("Does translation require an object to be selected?")]
        public LeanSelectable RequiredSelectable;
      
        public float Sharpness = 15.0f;

        // The position we still need to add
        private Vector3 remainingDelta;

        protected virtual void Update()
        {
            var screenPositionDelta = LeanTouch.DragDelta;

            if (RequiredSelectable != null)
            {
                if (RequiredSelectable.IsSelected == false)
                {
                    return;
                }

                if (RequiredSelectable.Finger != null)
                {
                    screenPositionDelta = RequiredSelectable.Finger.DeltaScreenPosition;
                }
            }

            Translate(screenPositionDelta);
        }

        protected virtual void LateUpdate()
        {
            // The framerate independent damping factor
            var factor = Mathf.Exp(-Sharpness * Time.deltaTime);

            // Dampen remainingDelta
            var newDelta = remainingDelta * factor;

            // Shift this transform by the change in delta
            transform.position += remainingDelta - newDelta;

            // Update remainingDelta with the dampened value
            remainingDelta = newDelta;
        }

        private void Translate(Vector2 screenPositionDelta)
        {
            // Store old position
            var oldPosition = transform.position;

            // Screen position of the transform
            var screenPosition = Camera.main.WorldToScreenPoint(oldPosition);
          
            // Add the deltaPosition
            screenPosition += (Vector3)screenPositionDelta;
          
            // Convert back to world space
            var newPosition = Camera.main.ScreenToWorldPoint(screenPosition);
            var delPosition = newPosition - oldPosition;

            // Add to delta
            remainingDelta += delPosition;
        }
    }
}

Well then, why not just put

if (Input.touches.Length < 2) return;

at the top of your rotation Update method?

Here is the Update method from my game where I have a single touch orbit, but a two-finger touch zoom. I had originally tried two-finger orbit, but my playtesters (wife and brother) found it unintuitive.

void Update() {
    bool doubleTapD = false;

    if (Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Ended) {
        if (Time.time < _doubleTapTimeD + 0.5f) {
            doubleTapD = true;
        }
        _doubleTapTimeD = Time.time;
    }

    if (doubleTapD) {
        Debug.Log("Changing camera perspective");
        if (isOrtho) {
            BlendToMatrix(perspective, 1f);
        } else {
            BlendToMatrix(ortho, 1f);
        }
        isOrtho = ! isOrtho;
        cameraComponent.orthographic = ! cameraComponent.orthographic;
    }

    if (Input.touchCount == 1 && Input.GetTouch (0).phase == TouchPhase.Moved) {
        // One finger, and it's moving
        //Debug.Log ("One finger swiping");
        Touch touch1 = Input.GetTouch (0);

        // Panning
        // Reverse rotation direction depending on top or bottom swipe
        if (touch1.position.y < Screen.height / 2) {
            velocityX += touch1.deltaPosition.x * xSpeed * distance * 0.02f;
            //Debug.Log("Swiping on bottom");
        } else {
            velocityX -= touch1.deltaPosition.x * xSpeed * distance * 0.02f;
            //Debug.Log("Swiping on top");
        }
        velocityY += touch1.deltaPosition.y * ySpeed * 0.02f;
       
    } else if (Input.touchCount == 2 && (Input.GetTouch (0).phase == TouchPhase.Moved || Input.GetTouch (1).phase == TouchPhase.Moved)) {
        // Two fingers
        //Debug.Log ("Two finger pinching");
        Touch touch1 = Input.GetTouch (0);
        Touch touch2 = Input.GetTouch (1);

        curDist = Vector2.Distance (touch1.position, touch2.position);
        float dotProduct = Vector2.Dot (touch1.deltaPosition.normalized, touch2.deltaPosition.normalized);

        // Pinching or un-pinching
        if (dotProduct <= -0.8) {
            // Zooming
            if (curDist > lastDist) {
                // Zoom out
                if (isOrtho) {
                    orthoSize -= Vector2.Distance (touch1.deltaPosition, touch2.deltaPosition) * pinchSpeed / 10;
                } else {
                    distance -= Vector2.Distance (touch1.deltaPosition, touch2.deltaPosition) * pinchSpeed / 10;
                }
            } else {
                // Zoom in
                if (isOrtho) {
                    orthoSize += Vector2.Distance (touch1.deltaPosition, touch2.deltaPosition) * pinchSpeed / 10;
                } else {
                    distance += Vector2.Distance (touch1.deltaPosition, touch2.deltaPosition) * pinchSpeed / 10;
                }
            }
            lastDist = curDist;
        }
    }
   
    rotationYAxis += velocityX;
    rotationXAxis -= velocityY;
   
    rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);

    Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
    Quaternion rotation = toRotation;
   
    // Clamp distance
    if (distance <= distanceMin) {
        //minimum camera distance
        distance = distanceMin;
    }
    if(distance >= distanceMax) {
        //maximum camera distance
        distance = distanceMax;
    }

    // Do same for ortho
    if (orthoSize <= orthoSizeMin) {
        orthoSize = orthoSizeMin;
    }
    if (orthoSize >= orthoSizeMax) {
        orthoSize = orthoSizeMax;
    }

    //Sets zoom
    if (isOrtho) {
        cameraComponent.orthographicSize = orthoSize;
    }

    transform.position = rotation * new Vector3(0.0f, 0.0f, -distance) + new Vector3(0f, 0f, 0f);
    transform.rotation = rotation;
   
    velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
    velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}

Should be pretty easy to adapt this to do what you’re going for here.

1 Like

EDIT: Wow, somehow managed to post to a completely different thread than I intended. Oops. Moving on…

1 Like

Thank you friend

This is bad now i can drag object at same time rotating it. its crazy. Need to fix it. I really need help on this.