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 are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here:
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

touch-to-orbit script

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

  1. mindlube


    Oct 3, 2008
    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
    3. // min and max height of the platter:
    4. var minHeight : float = 0.2;
    5. var maxHeight : float = 3.0;
    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;
    12. function Update ()
    13. {
    14.     var newHeight : float;
    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);
    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


    Oct 3, 2008
    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


    May 21, 2009
    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


    Aug 26, 2008
    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


    Apr 5, 2009
    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


    Jul 17, 2009
    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]