Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

touch-to-orbit script

Discussion in 'iOS and tvOS' started by mindlube, Nov 20, 2008.

  1. mindlube

    mindlube

    Joined:
    Oct 3, 2008
    Posts:
    993
    This is a really simple way to control a camera and light via touch events.

    Create a gameobject with an invisible cube or plane. Add a camera to an edge of the cube (not the center), and add a directional light elsewhere on the cube. Add to the camera the standard SmoothLookAt script, and point it to an object somewhere under the platter.

    This cube or plane is the platter (dolly?) that controls your light and camera. Add the following script to it.

    This script spins the camera platter (the cube, not the camera itself) in response to horizontal swipes, and elevates/lowers the platter in response to vertical swipes. It's a pretty intuitive way to navigate! Hope someone finds this useful :D

    Code (csharp):
    1. #pragma strict
    2.  
    3. // min and max height of the platter:
    4. var minHeight : float = 0.2;
    5. var maxHeight : float = 3.0;
    6.  
    7. // rate of elevation in transform position per unit of touch-movement:
    8. var elevationRate : float = 0.01;
    9. // rotation rate in degrees per unit of touch-movement:
    10. var rotationRate : float = 0.3;
    11.  
    12. function Update ()
    13. {
    14.     var newHeight : float;
    15.    
    16.     for (var evt : iPhoneTouch in iPhoneInput.touches)
    17.     {
    18.         if (evt.phase == iPhoneTouchPhase.Moved)
    19.         {          
    20.             // map touches X-delta to the platter rotation (around Y)
    21.             transform.Rotate(Vector3.up * evt.positionDelta.x * rotationRate);
    22.            
    23.             // map touches Y-delta to the platter height (+/-Y)
    24.             newHeight = transform.position.y + (evt.positionDelta.y * elevationRate);
    25.             transform.position.y = Mathf.Clamp( newHeight, minHeight, maxHeight);
    26.         }
    27.     }
    28. }
    edited for clarity and typos
     
  2. mindlube

    mindlube

    Joined:
    Oct 3, 2008
    Posts:
    993
    Actually I think it's more intuitive to reverse the direction of the camera height control:
    newHeight = transform.position.y + (evt.positionDelta.y * elevationRate * -1.0);
     
  3. Chowderman

    Chowderman

    Joined:
    May 21, 2009
    Posts:
    92
    This is a fantastic script, thankyou so much! I have a question though. Any idea how to keep the camera moving with momentum computed from finger speed? Allowing the user to "flick" the camera and have it continue to spin until it slowly comes to a stop. I realize that's way more complex, involving momentum, slowdown speed, etc but I'm curious as to how it's done.

    I'm a total Unity noob so take it slow! :p
     
  4. gwarsh41

    gwarsh41

    Joined:
    Aug 26, 2008
    Posts:
    44
    I am also trying to figure out the "flick" for my next app. If you could go over it, I would be very happy!
     
  5. HanulTech

    HanulTech

    Joined:
    Apr 5, 2009
    Posts:
    312
    Someone just asked a similar question on another thread, but you should be be able to this by taking a time stamp for the touch when the gesture begins and then when it ends. Based on the elapsed time and distance, you can figure of the speed of the swipe at gesture end, and then use that information to move the camera further.
     
  6. KITT

    KITT

    Joined:
    Jul 17, 2009
    Posts:
    221
    For momentum instead of straight rotation, I would give the target object a Rigidbody, untick "Use Gravity" and add a subtle Angular Drag to the Rigidbody settings so that is slows down when you release the drag. Append the code with this -

    Code (csharp):
    1. transform.rigidbody.AddTorque(0, evt.deltaPosition.x * -rotationRate, 0);
    in place of

    Code (csharp):
    1. transform.Rotate(Vector3.up * evt.positionDelta.x * rotationRate);
    [P.S - I am using Unity 3 where positionDelta now = deltaPosition]
     
unityunity