Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Accelerometer smoothing

Discussion in 'Scripting' started by jacksu456, Sep 6, 2017.

  1. jacksu456

    jacksu456

    Joined:
    Nov 16, 2016
    Posts:
    30
    This is my code for a block to move with the accelerometer, but it think its not smooth enough, how could i change it?

    Code (CSharp):
    1. void MoveLeft ()
    2.     {
    3.         transform.Translate(Vector3.left * Time.deltaTime * VelocityCube, Space.World);
    4.     }
    5.  
    6.     void MoveRight ()
    7.     {
    8.         transform.Translate(Vector3.right * Time.deltaTime * VelocityCube, Space.World);
    9.     }
    10.  
    11.     void Accelerometer ()
    12.     {
    13.         float x = Input.acceleration.x;
    14.         if (x < -0.1f) {
    15.             MoveLeft ();
    16.         } else if (x > 0.1f) {
    17.             MoveRight ();
    18.         }
    19.     }
    20.  
    21.     void FixedUpdate ()
    22.     {
    23.         Accelerometer ();
    24.     }
     
  2. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    992
    Put it in Update rather than FixedUpdate. FixedUpdate should be used exclusively for physics calls.
    e.g. everything that has to do with adding force to rigidbodies.
     
    sdutter likes this.