Search Unity

Scroll Wheel Replacement for Touch Screen(Android)

Discussion in 'Scripting' started by Timmer1992, Mar 6, 2011.

  1. Timmer1992

    Timmer1992

    Joined:
    Mar 6, 2011
    Posts:
    1
    I have a script that is edited from the original MouseOrbit.js in Unity, to include zooming with the Scroll Wheel, Below:
    Code (csharp):
    1. var target : Transform;
    2. var distance = 10.0;
    3. var cameraSpeed = 5;
    4.  
    5. var xSpeed = 175.0;
    6. var ySpeed = 75.0;
    7.  
    8. var yMinLimit = 20; //Lowest vertical angle in respect with the target.
    9. var yMaxLimit = 80;
    10.  
    11. var minDistance = 5; //Min distance of the camera from the target
    12. var maxDistance = 20;
    13.  
    14. private var x = 0.0;
    15. private var y = 0.0;
    16.  
    17. function Start () {
    18.     var angles = transform.eulerAngles;
    19.     x = angles.y;
    20.     y = angles.x;
    21.  
    22.    // Make the rigid body not change rotation
    23.       if (rigidbody)
    24.       rigidbody.freezeRotation = true;
    25. }
    26.  
    27. function Update () {
    28.     if (target  camera) {
    29.  
    30.    //Zooming with mouse
    31.    distance += Input.GetAxis("Mouse ScrollWheel")*distance*-1;
    32.    distance = Mathf.Clamp(distance, minDistance, maxDistance);
    33.  
    34.    //Detect mouse drag;
    35.    if(Input.GetMouseButton(0))   {
    36.    
    37.         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
    38.         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;      
    39.       }
    40.       y = ClampAngle(y, yMinLimit, yMaxLimit);
    41.              
    42.         var rotation = Quaternion.Euler(y, x, 0);
    43.         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    44.          
    45.    transform.position = Vector3.Lerp (transform.position, position, cameraSpeed*Time.deltaTime);
    46.       transform.rotation = rotation;      
    47.     }
    48. }
    49.  
    50. static function ClampAngle (angle : float, min : float, max : float) {
    51.    if (angle < -360)
    52.       angle += 360;
    53.    if (angle > 360)
    54.       angle -= 360;
    55.    return Mathf.Clamp (angle, min, max);
    56. }
    Like I said there is nothing wrong with it but i would like to use it on an Android device to control the camera around a simple 3D object, for people to get a closer look at some of my work, i would like to find a way to scroll on the Android Touch Screen, By either placing a simple GUI Slider for zooming or actually Changing the script to allow a "Pinch" for zooming.

    A "Pinch" Is placing two fingers on the touch screen, and sliding them together for zooming in or further appart for zooming out.

    Please help.
    Thanks.

    PS: I am a n00b at this, dont hate me we were all there before.
     
    Last edited: Mar 6, 2011
  2. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    865
    let me know if you found a solution for this!