Question by 
               yaswong135 · Jun 25, 2019 at 02:58 PM · 
                inputtouchscreenorbitdragging  
              
 
              Orbit for touchscreen
I am trying to create a scene where a user can rotate a skull and move it and zoom in and out on a touchscreen tablet. I currently seem to be able to get it to work with my mouse and scroll wheel, but it doesn't work when on a touchscreen, despite other elements of the UI working with touchscreen input. Have I missed something?
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [AddComponentMenu("Camera-Control/Mouse drag Orbit with zoom")]
 public class OrbitSkull : MonoBehaviour
 {
     public Transform target;
     public float distance = 500.0f;
     public float xSpeed = 0.1f;
     public float ySpeed = 10.0f;
     
     public float yMinLimit = -20f;
     public float yMaxLimit = 80f;
     
     public float distanceMin = 200f;
     public float distanceMax = 500f;
     
     public float smoothTime = 2f;
 
     public float zoomSpeed = 100.0f;
     
     float rotationYAxis = 0.0f;
     float rotationXAxis = 0.0f;
     
     float velocityX = 0.0f;
     float velocityY = 0.0f;
 
     public bool detectColliders = false;
 
  
     
     // Use this for initialization
     void Start()
     {
         Vector3 angles = transform.eulerAngles;
         rotationYAxis = angles.y;
         rotationXAxis = angles.x;
         
         // Make the rigid body not change rotation
         if (GetComponent<Rigidbody>())
         {
             GetComponent<Rigidbody>().freezeRotation = true;
         }
     }
 
     // Late update happens after the normal update, and is used to make sure other 
     // update events have happened first
     void LateUpdate()
     {
         if (target)
         {
              
      if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved) 
  
      {
  
          //One finger touch does orbit
  
          touch = Input.GetTouch(0);
  
          x += touch.deltaPosition.x * xSpeed * 0.02f;
  
          y -= touch.deltaPosition.y * ySpeed * 0.02f;
  
      }
  
      if (Input.touchCount > 1 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved))
             /* if (Input.GetMouseButton(0))
             {
                 velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
                 velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
             }
             */
             rotationYAxis += velocityX;
             rotationXAxis -= velocityY;
             
             rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
             
             Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
             Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
             Quaternion rotation = toRotation;
             
             distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * zoomSpeed, distanceMin, distanceMax);
 
 
             if (detectColliders) {
                 RaycastHit hit;
                 if (Physics.Linecast(target.position, transform.position, out hit))
                 {
                     distanceMin = hit.distance;
                 }
             }
             Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
             Vector3 position = rotation * negDistance + target.position;
             
             transform.rotation = rotation;
             transform.position = position;
             
             velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
             velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
         }
         
     }
 
     // Clamp angle make sure the orbit does not flip the object upside down
     public static float ClampAngle(float angle, float min, float max)
     {
         if (angle < -360F)
             angle += 360F;
         if (angle > 360F)
             angle -= 360F;
         return Mathf.Clamp(angle, min, max);
     }
 }
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
Touchscreen orbit 0 Answers
Android| Camera scrolling 1 Answer
Touch on 2D gameobject: should i use a GUI button? 1 Answer
Input.GetTouch(0).deltaPosition for rollout on orbit camera behaving strangly 0 Answers
Touch input not working 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                