Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

MouseOrbit not working with touch drag

Discussion in 'Scripting' started by joyceanblue, Mar 3, 2012.

  1. joyceanblue

    joyceanblue

    Joined:
    Jul 26, 2010
    Posts:
    43
    hi folks, I am trying to use the MouseOrbit script, it works fine if i use my mouse (click and drag to orbit) but i got this touchscreen and when i touch and drag on screen it does nothing. its a "single touch" screen, im using the touch to emulate mouse click, it works fine in other places such as clicking a button etc. any idea how to fix this problem?
    btw i tried another script by cyb3rmaniak from this thread
    http://forum.unity3d.com/threads/19846-Smooth-Camera-Orbit-Script-on-mouse-click-%28help%29
    and it works with touch drag but it resets the cam position if i retouch the screen.
    Code (csharp):
    1. var target : Transform;
    2. var distance = 10.0;
    3.  
    4. var xSpeed = 250.0;
    5. var ySpeed = 120.0;
    6.  
    7. var yMinLimit = -20;
    8. var yMaxLimit = 80;
    9.  
    10. private var x = 0.0;
    11. private var y = 0.0;
    12.  
    13. @script AddComponentMenu("Camera-Control/Mouse Orbit")
    14.  
    15. function Start () {
    16.     var angles = transform.eulerAngles;
    17.     x = angles.y;
    18.     y = angles.x;
    19.  
    20.     // Make the rigid body not change rotation
    21.     if (rigidbody)
    22.         rigidbody.freezeRotation = true;
    23. }
    24.  
    25. function LateUpdate () {
    26.  
    27.             if(Input.GetMouseButton(0)) { //Mouse button is down
    28.                 //Change the angles by the mouse movement
    29.                 x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
    30.                 y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
    31.         y = ClampAngle(y, yMinLimit, yMaxLimit);
    32.                    
    33.         var rotation = Quaternion.Euler(y, x, 0);
    34.         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    35.        
    36.         transform.rotation = rotation;
    37.         transform.position = position;
    38.        
    39.                     //Zooming with mouse
    40.     distance += Input.GetAxis("Mouse ScrollWheel")*distance;
    41.     }
    42.    
    43.  
    44. }
    45.  
    46. static function ClampAngle (angle : float, min : float, max : float) {
    47.     if (angle < -360)
    48.         angle += 360;
    49.     if (angle > 360)
    50.         angle -= 360;
    51.     return Mathf.Clamp (angle, min, max);
    52. }
     
  2. joyceanblue

    joyceanblue

    Joined:
    Jul 26, 2010
    Posts:
    43
    I got this idea from another post and wrote the following script, it partially solved my problem, the touch drag works but now i have another problem, if i take my finger off or even stop dragging the mouse (button down) and then on the next touch or click, it doesnt read it as a neutral point but instead the moment you do the next touch or click it starts to orbit relative to the first click/touch that you did the moment simulation started. any suggestions?

    Code (csharp):
    1. var target : Transform;
    2.  
    3. var distance = 10.0;
    4.  
    5. var xSpeed = 250.0;
    6.  
    7. var ySpeed = 120.0;
    8.  
    9. var startX;
    10.  
    11. var startY;
    12.  
    13. var yMinLimit = -20;
    14.  
    15. var yMaxLimit = 80;
    16.  
    17. private var x = 0.0;
    18.  
    19. private var y = 0.0;
    20.  
    21.  
    22.  
    23. @script AddComponentMenu("Camera-Control/Mouse Orbit")
    24.  
    25.  
    26.  
    27. function Start () {
    28.  
    29.     startX = Input.mousePosition.x;
    30.  
    31.     startY = Input.mousePosition.y;
    32.  
    33.     var angles = transform.eulerAngles;
    34.  
    35.     x = angles.y;
    36.  
    37.     y = angles.x;
    38.  
    39.  
    40.  
    41.     // Make the rigid body not change rotation
    42.  
    43.     if (rigidbody)
    44.  
    45.         rigidbody.freezeRotation = true;
    46.  
    47. }
    48.  
    49.  
    50.  
    51. function LateUpdate () {
    52.  
    53.  
    54.  
    55.          if(Input.GetMouseButton(0)) { //Mouse button is down
    56.  
    57.                  
    58.  
    59.             var currentX = Input.mousePosition.x;
    60.  
    61.             var differenceX = currentX-startX;
    62.  
    63.             var currentY = Input.mousePosition.y;
    64.  
    65.             var differenceY = currentY-startY;
    66.  
    67.                 x +=  differenceX*xSpeed*Time.deltaTime*0.01;
    68.  
    69.                 y -=  differenceY*ySpeed*Time.deltaTime*0.01;
    70.  
    71.                      
    72.  
    73.         y = ClampAngle(y, yMinLimit, yMaxLimit);
    74.  
    75.                    
    76.  
    77.         var rotation = Quaternion.Euler(y, x, 0);
    78.  
    79.         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    80.  
    81.        
    82.  
    83.         transform.rotation = rotation;
    84.  
    85.         transform.position = position;
    86.  
    87.        
    88.  
    89.                     //Zooming with mouse
    90.  
    91.     distance += Input.GetAxis("Mouse ScrollWheel")*distance;
    92.  
    93.     }
    94.  
    95. }
    96.  
    97.  
    98.  
    99. static function ClampAngle (angle : float, min : float, max : float) {
    100.  
    101.     if (angle < -360)
    102.  
    103.         angle += 360;
    104.  
    105.     if (angle > 360)
    106.  
    107.         angle -= 360;
    108.  
    109.     return Mathf.Clamp (angle, min, max);
    110.  
    111. }
     
  3. joyceanblue

    joyceanblue

    Joined:
    Jul 26, 2010
    Posts:
    43
    adding the following code solved the problem for the mouse but not with the touch. for touch i have to touch twice in the new places to initiate orbiting from that point... weird
    Code (csharp):
    1.  else if (Input.GetMouseButtonUp) {
    2.  
    3.     startX = Input.mousePosition.x;
    4.  
    5.     startY = Input.mousePosition.y;
    6.  
    7.     }
    p.s. why is that copy pasting code add extra blank line in between two lines.