Search Unity

Slow down camera movement

Discussion in 'Scripting' started by herbie, Feb 19, 2014.

  1. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    With this code I can move the camera in my Android game with one finger.
    Code (csharp):
    1. function Update ()
    2. {
    3.     if (Input.touchCount == 1  Input.GetTouch(0).phase == TouchPhase.Moved)
    4.     {
    5.         if (Input.GetTouch(0).deltaPosition.x < 0)
    6.         {
    7.             this.transform.position.x -= -Input.GetTouch(0).deltaPosition.x * dragSpeed;
    8.         }
    9.        
    10.         if (Input.GetTouch(0).deltaPosition.x > 0)
    11.         {
    12.             this.transform.position.x -= -Input.GetTouch(0).deltaPosition.x * dragSpeed;
    13.         }
    14.     }  
    15. }
    When I release my finger, it stops moving immediately.
    But what I would like to have is that the camera is slowing down after releasing my finger. How can I do that?
     
  2. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    I think I need at least the speed of the touch:
    Code (csharp):
    1. speedTouch = Input.GetTouch(0).deltaPosition.x;
    But how can I slow down this speed to zero? Something with Lerp?
     
  3. Duney

    Duney

    Joined:
    Aug 19, 2013
    Posts:
    170
    Could always have an else statement that says if there is no touchCount, check the speed is not 0, then gradually reduces the speed (possibly with a lerp).
     
  4. QI

    QI

    Joined:
    Oct 27, 2012
    Posts:
    229
    Hi,I have a idea that using inertia.

    Look at this code:

    Code (csharp):
    1. Vector3 curInertia = Vector3.zero;
    2.  
    3. void Update(){
    4.     if(Input.GetKey(KeyCode.W)) SetMove(Vector3.forward);
    5.     if(Input.GetKey(KeyCode.S)) SetMove(Vector3.back);
    6.     if(Input.GetKey(KeyCode.A)) SetMove(Vector3.left);
    7.     if(Input.GetKey(KeyCode.D)) SetMove(Vector3.right);
    8. }
    9.  
    10. void SetMove(Vector3 dir){
    11.     curInertia = dir;
    12. }
    13.  
    14. IEnumerator Move(){
    15.     thisT.hasChanged = true;
    16.     while (true) {
    17.         thisT.Translate (curInertia * mMoveSpeed * Time.deltaTime, Space.World);
    18.  
    19.         if(thisT.hasChanged){
    20.             curInertia *= moveInertia*Time.deltaTime;
    21.             thisT.hasChanged = false;
    22.         }
    23.  
    24.         yield return null;
    25.     }
    26. }
    A simple example that show you how to make your movement have a smooth inertia.
    Best effect is set moveInertia to 55.
    But this algorithm have a mistake that if moveInertia*Time.deltaTime > 1,then your camera will never stop.
    Solve this is easy,just lock inertia in -1 ~ 1.
    But if you do this ,inertia will never greater than some any value.

    Until now,I don't have a prefect solution

    And I will tell you if I got it !!
     
  5. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Thanks for reply.
    This will help me!
     
  6. wandern3D

    wandern3D

    Joined:
    Oct 13, 2013
    Posts:
    64
    Hi Herbie,
    I am not shure but when you start a timer like this:
    Code (csharp):
    1.  
    2. // set dragspeed_timer in Start function to a value you want.
    3. dragspeed_timer -= Time.deltaTime;
    4. if (dragspeed_timer < 0) DoSomething;
    5.  
    dragSpeed = dragspeed_timer -> So the variable dragSpeed should go to cero
     
    Last edited: Feb 20, 2014
  7. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    I will try that.
    Thanks!
     
  8. QI

    QI

    Joined:
    Oct 27, 2012
    Posts:
    229
    Hi,
    You let me know that.
    Just make "curInertia" to an float,and -= Time.deltaTime*moveInertia every game loop.
    And move by *curInertia.
    That's great!!
    Thanks you
     
  9. Maximillion

    Maximillion

    Joined:
    Dec 20, 2013
    Posts:
    22
    Awful but could be altered to be nice? (it will probably need alot of tweaking as i just quickly wrote this on my phone... Like making the speed dynamic each update instead of at load etc)

    Code (csharp):
    1.  
    2.  
    3.     var countDown : float;
    4.     var count : float;
    5.     var dir : String;
    6.  
    7.     function Start ()
    8.     {
    9.         count = dragSpeed;
    10.         countDown = dragSpeed / 4;
    11.         dir = "n";
    12.     {
    13.  
    14.     function Update ()
    15.     {
    16.         if (Input.touchCount == 1  Input.GetTouch(0).phase == TouchPhase.Moved)
    17.         {
    18.             count = dragSpeed;
    19.             if (Input.GetTouch(0).deltaPosition.x < 0)
    20.             {
    21.                 this.transform.position.x += Input.GetTouch(0).deltaPosition.x * count;
    22.                 dir = "r";
    23.             }
    24.            
    25.             if (Input.GetTouch(0).deltaPosition.x > 0)
    26.             {
    27.                 this.transform.position.x -= Input.GetTouch(0).deltaPosition.x * count;
    28.                 dir = "l";
    29.             }
    30.         }
    31.         else if (Input.touchCount == 0)
    32.         {
    33.             if (count > 0)
    34.             {
    35.                 if (dir == "r")
    36.                 {
    37.                     this.transform.position.x += Input.GetTouch(0).deltaPosition.x * count * Time.deltaTime;
    38.                 }
    39.            
    40.                 if (dir == "l")
    41.                 {
    42.                     this.transform.position.x -= Input.GetTouch(0).deltaPosition.x * count * Time.deltaTime;
    43.                 }
    44.                 count -= countDown * Time.deltaTime;
    45.             }
    46.             else
    47.             {
    48.                 count = dragSpeed;
    49.                 dir = "n";
    50.             }
    51.         }
    52.         else
    53.         {
    54.             dir = "n";
    55.         }
    56.     }
    57.  
     
    Last edited: Feb 21, 2014
  10. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Code (csharp):
    1.  
    2. class Movement : MonoBehaviour
    3. {
    4.   float dragX = 0;
    5.   float mass = 0.2f;
    6.   bool dragging = false;
    7.   float prevX = 0;
    8.  
    9.  
    10. void OnEnable()
    11. {
    12.    StartCoroutine(Inertia());
    13.    StartCoroutine(Input());
    14. }
    15.  
    16.   IEnumerator Inertia()
    17.  {
    18.    while(enabled)
    19.   {
    20.  
    21.     if(!dragging)
    22.     dragX = Mathf.moveTowards(dragX,0,mass * Time.deltaTIme);
    23.  
    24.  
    25.     yield return null;
    26.    }
    27.  
    28.   yield return null;
    29.  }
    30.   IEnumerator  Input()
    31.   {
    32.  
    33.     while(enabled)
    34.      {
    35.        
    36.        
    37.         dragX = Input.GetTouch(0).deltaPosition.x * dragSpeed;
    38.  
    39.          dragging = (prevX != dragX);
    40.          prevX = dragX;
    41.         yield return null;
    42.      }
    43.  
    44.    yield return null;
    45.   }
    46. }
    47.  

    Untested, but a good start to what you want.
     
  11. QI

    QI

    Joined:
    Oct 27, 2012
    Posts:
    229
    Yeah I got while(enable) is good than while(true).

    :)