2
\$\begingroup\$

I'm working on maze game for Android in Unity 5.1.1f1 and I have troubles with controlling my ball with accelerometer. At start I tried:

public class PlayerController : MonoBehaviour {

    public GameObject sphere;
    public Camera camera;
    public float speed=200;

    private Rigidbody myRigidBody;

    void Start()
    {
        myRigidBody = gameObject.GetComponent<Rigidbody> ();
    }


    void FixedUpdate() 
    {
        float moveH = Input.acceleration.x;
        float moveV = -Input.acceleration.z;

        Vector3 move = new Vector3 (moveH, 0.0f, moveV);
        myRigidBody.AddForce (move * speed*Time.deltaTime);
    }   
}

But the ball is not moving as it should. Then I tried another solution. But my ball still doesn't move right. It seems like it's sometimes hard to move left/right/forward/backward, also it's possible my ball will rotate in the opposite direction.

public class PlayerController : MonoBehaviour {


    public float speedAc = 10;

    //accelerometer
    private Vector3 zeroAc;
    private Vector3 curAc;
    private float sensH = 10;
    private float sensV = 10;
    private float smooth = 0.5f;
    private float GetAxisH = 0;
    private float GetAxisV = 0;

    // Use this for initialization
    void Start () {

        ResetAxes();
    }

    //accelerometer
    void ResetAxes(){
        zeroAc = Input.acceleration;
        curAc = Vector3.zero;
    }

    void FixedUpdate () {

        curAc = Vector3.Lerp(curAc, Input.acceleration-zeroAc, Time.deltaTime/smooth);

        GetAxisH = Mathf.Clamp(curAc.x * sensH, -1, 1);
        GetAxisV = Mathf.Clamp(-curAc.z * sensV, -1, 1);

        Vector3 movement = new Vector3 (GetAxisH, 0.0f, GetAxisV);
        GetComponent<Rigidbody>().AddForce(movement * speedAc);
    }       
}

Can someone help me, please?

\$\endgroup\$
1

1 Answer 1

1
\$\begingroup\$

The third parameter of Vector3.Lerp, which named "t", does not stand for "time". It should be between 0 and 1, and gives results as below:

curAc = Vector3.Lerp(curAc, Input.acceleration-zeroAc, 0);    //curAc = curAc
curAc = Vector3.Lerp(curAc, Input.acceleration-zeroAc, 1);    //curAc = Input.acceleration-zeroAc
curAc = Vector3.Lerp(curAc, Input.acceleration-zeroAc, 0.5f); //curAc = (Input.acceleration-zeroAc + curAc)/2     

Here is a good explanation of Lerp: http://answers.unity3d.com/answers/998744/view.html

\$\endgroup\$
4
  • \$\begingroup\$ Thank you, I didn't know that because I'm still new to Unity... But now I'm wondering what to use instead of Time.deltaTime/smooth? \$\endgroup\$
    – Nami
    Commented Nov 2, 2015 at 9:54
  • \$\begingroup\$ If one is aware that Lerp clamps at the top value if the t parameter is > 1 then it's legit and quite common to use Time.deltaTime as lerping factor. \$\endgroup\$ Commented Nov 2, 2015 at 10:23
  • \$\begingroup\$ @SteakOverflow Then what you expect to get with that statement? What if your values hits over 1 several times in a row? Then your whole smoothing process will become some kind of slide-show. \$\endgroup\$ Commented Nov 2, 2015 at 11:08
  • \$\begingroup\$ When Time.deltaTime > 1, all sorts of things can be done, depending on your goals. Time.deltaTime > 1 is very unlikely to happen when the game is running properly, so if that happens it's reasonable to jump to code that manages a malfunction. It also makes sense to use deltaTime as lerping value in FixedUpdate where deltaTime is constant. Lerping isn't used just for smoothing, it's a general interpolation method. \$\endgroup\$ Commented Nov 2, 2015 at 11:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .