Search Unity

How to rotate a 'dial' ?

Discussion in 'Scripting' started by D_e_l_t_a, Aug 29, 2009.

  1. D_e_l_t_a

    D_e_l_t_a

    Joined:
    Jan 2, 2009
    Posts:
    72
    Hi,

    I'm trying to simulate a 'jog' dial, like the dial you get on a stereo or mixing desk. It needs to rotate in a clockwise anti-clockwise manner in response to an iPhone swipe gesture, that is it should continue spinning for a second after swiping/releasing before slowing to zero rotation, but should stop dead on a non-swipe touch release.

    I'm not at my Unity computer right now but my code so far just has a disc to act as a dial which turns left/right on a finger swipe, but I can't seem to get the 'accelerated' dial-turn movement described above.

    Any suggestions?
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    use physics to turn it then the physics will handle it. otherwise you will have to replicate what the physics does by giving it a "turn power" that degrades continously
     
  3. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Why not base how far the dial turns based on iPhoneTouch.deltaPosition.x, and if iPhoneTouch.phase == iPhoneTouchPhase.Ended, lerp the amount the dial turns down to zero based on a time value that is proportional to iPhoneTouch.deltaPosition.x?
     
  4. D_e_l_t_a

    D_e_l_t_a

    Joined:
    Jan 2, 2009
    Posts:
    72
    The deltaPosition method sounds interesting. I've been using Unity iPhone for just a few days though, I'd like to get to grips with using the delta. data;
    If anyone can throw some example code up that I can fidle with I'd be grateful, particularly along the lines of spinning/rotating an object with the touchscreen.
     
  5. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    file:///Applications/Unity%20iPhone/Unity%20iPhone.app/Contents/Documentation/Documentation/ScriptReference/Transform.RotateAround.html

    Code (csharp):
    1. function Update()
    2.  {
    3.      transform.RotateAround (transform.position, Vector3.forward, iPhoneInput.GetTouch(0).deltaPosition.x);
    4. }
     
  6. D_e_l_t_a

    D_e_l_t_a

    Joined:
    Jan 2, 2009
    Posts:
    72
    Ok, I'm having a little trouble with the code here. I tried this example from the manual:

    // Moves object according to finger movement on the screen
    var speed:float = 0.1;
    function Update () {
    if (iPhoneInput.touchCount > 0 iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Moved) {

    // Get movement of the finger since last frame
    var touchDeltaPosition:Vector2 = iPhoneInput.GetTouch(0).deltaPosition;

    // Move object across XY plane
    transform.Translate (-touchDeltaPosition:Vector2.x * speed, -touchDeltaPosition:Vector2.y * speed, 0);
    }
    }
    and I get a "expecting ) found :" error in the transform.Translate line of code.

    I can't seem to get this code working ?
     
  7. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Please use the Code button (found to the right of "Message body") to make the code easier to read. Like this! :D

    Code (csharp):
    1. // Moves object according to finger movement on the screen
    2. var speed:float = 0.1;
    3.  
    4. function Update ()
    5. {
    6.     if (iPhoneInput.touchCount > 0  iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Moved)
    7.     {
    8.         // Get movement of the finger since last frame
    9.         var touchDeltaPosition : Vector2 = iPhoneInput.GetTouch(0).deltaPosition;
    10.        
    11.         // Move object across XY plane
    12.         transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
    13.     }
    14. }
    Your problem was that you were putting the ": Vector2" after touchDeltaPosition, twice, after it had already been defined as a Vector2.

    Ouch, that's really in the manual as is! :x I'm reporting a bug about the documentation right now.
     
  8. D_e_l_t_a

    D_e_l_t_a

    Joined:
    Jan 2, 2009
    Posts:
    72
    I didn't think it read right but I convinced myself it was....this vector stuff is rather new and I'm up to my neck in code and thinking....the manual is always right!

    Thanks though Jessy, i'm making ground on this now.
     
  9. Wozik

    Wozik

    Joined:
    Apr 10, 2009
    Posts:
    662
    thanks for reporting. this glitch will be eliminated in the next release