Unity - 如何让物体的加速度计在智能手机中移动得更顺畅?

Unity - How to make the accelerometer of object move more smoothly in smartphone?

我想让我的角色在倾斜 phone 时平稳移动。我怎样才能让它平稳移动并且速度和速度随着 phone?

的斜率而增加
void AccelerometerMove(){

float x = Input.acceleration.x;
Debug.Log("X = " + x);

if (x < -0.1f)
{
    MoveLeft();
}
else if (x > 0.1f)
{
    MoveRight();
}
else
{
    SetVelocityZero();
}
}
public void SetVelocityZero()
{
     rb.velocity = Vector2.zero;
}

public void MoveLeft()
{
rb.velocity = new Vector2(-speed, 0);
//transform.Translate(Vector2.right * speed * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 180);
}

public void MoveRight()
{
rb.velocity = new Vector2(speed, 0);
//transform.Translate(Vector2.right * speed * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0);
}

您可以直接使用加速度计的输入来设置物体的速度:

void AccelerometerMove()
{
    float x = Input.acceleration.x;
    Debug.Log("X = " + x);

    if (x < -0.1f)
    {
        MoveLeft(x);
    }
    else if (x > 0.1f)
    {
        MoveRight(x);
    }
    else
    {
        SetVelocityZero();
    }
}
public void SetVelocityZero()
{
     rb.velocity = Vector2.zero;
}

public void MoveLeft( float s )
{
    rb.velocity = new Vector2(s, 0);
    transform.eulerAngles = new Vector2(0, 180);
}

public void MoveRight( float s )
{
    rb.velocity = new Vector2(s, 0);
    transform.eulerAngles = new Vector2(0, 0);
}

和/或使用Mathf.Lerp function计算速度:

void AccelerometerMove()
{
    float x = Input.acceleration.x;
    Debug.Log("X = " + x);

    if (x < -0.1f)
    {
        MoveLeft();
    }
    else if (x > 0.1f)
    {
        MoveRight();
    }
    else
    {
        SetVelocityZero();
    }
}
public void SetVelocityZero()
{
     rb.velocity = Vector2.zero;
}

public void MoveLeft()
{
    rb.velocity = new Vector2( Mathf.Lerp( rb.velocity.x, -speed, Time.deltaTime ), 0);
    transform.eulerAngles = new Vector2(0, 180);
}

public void MoveRight()
{
    rb.velocity = new Vector2( Mathf.Lerp( rb.velocity.x, speed, Time.deltaTime ), 0);
    transform.eulerAngles = new Vector2(0, 0);
}