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. Dismiss Notice

Using touch to rotate around an object

Discussion in 'iOS and tvOS' started by Hesham, Oct 1, 2010.

  1. Hesham

    Hesham

    Joined:
    May 29, 2008
    Posts:
    147
    I've been trying to have my camera rotate around an object using screen touch.
    I haven't been successful so far though I have based my script on the MouseOrbit script that comes with unity.
    Would appreciate if some can give tips of hints on how to do this.

    Sorry if the formatting so off, I'm posting from my iPhone.
     
  2. Hesham

    Hesham

    Joined:
    May 29, 2008
    Posts:
    147
    here is how I finally implemented it:

    Code (csharp):
    1.  
    2. var target : Transform;
    3. var distance = 10.0;
    4.  
    5. var xSpeed = 250.0;
    6. var ySpeed = 120.0;
    7.  
    8. var yMinLimit = -20;
    9. var yMaxLimit = 80;
    10.  
    11. private var x = 0.0;
    12. private var y = 0.0;
    13.  
    14. var xsign =1;
    15.  
    16. @script AddComponentMenu("Camera-Control/Mouse Orbit")
    17.  
    18. function Start () {
    19.     var angles = transform.eulerAngles;
    20.     x = angles.y;
    21.     y = angles.x;
    22.    
    23.     var rotation = Quaternion.Euler(y, x, 0);
    24.     var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    25.        
    26.         transform.rotation = rotation;
    27.         transform.position = position;
    28.  
    29.    
    30. }
    31.  
    32. function LateUpdate () {
    33.    
    34.    
    35.     //get the rotationsigns
    36.    
    37.     var forward = transform.TransformDirection(Vector3.up);
    38.     var forward2 = target.transform.TransformDirection(Vector3.up);
    39.      if (Vector3.Dot(forward,forward2) < 0)
    40.             xsign = -1;
    41.             else
    42.             xsign =1;
    43.    
    44.    
    45.     for (var touch : Touch in Input.touches) {
    46.     if (touch.phase == TouchPhase.Moved) {
    47.         x += xsign * touch.deltaPosition.x * xSpeed *0.02;
    48.         y -= touch.deltaPosition.y * ySpeed *0.02;
    49.        
    50.        
    51.                
    52.         var rotation = Quaternion.Euler(y, x, 0);
    53.         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    54.        
    55.         transform.rotation = rotation;
    56.         transform.position = position;
    57.     }
    58.     }
    59. }
    60.  
    61.  
    62.  
     
  3. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,321
    Thank you, that's useful :)
     
  4. ANTA

    ANTA

    Guest

    Joined:
    Mar 13, 2010
    Posts:
    5
    @Hesham
    I'm also interested in this topic. Would you like to explain more? or write a short tutorial for me please!
     
  5. kayahan

    kayahan

    Joined:
    Dec 6, 2010
    Posts:
    1
    This is exactly what i am looking for. Thank you Hesham. But, unfortunately i can not implement this. There is no error message when i run. Do i need to add some package to use this script?

    Thanks again.

    Regards.
     
  6. Ahmed Rabah

    Ahmed Rabah

    Joined:
    Feb 9, 2012
    Posts:
    18
    Thanks Hesham you just made my day :)
     
  7. Nabil_fx

    Nabil_fx

    Joined:
    Nov 9, 2013
    Posts:
    124
    i need to change the code to Touch the object, to rotate, how can i figure that. thanks...