Search Unity

How to move Object using accelerometer?

Discussion in 'Android' started by jerryberry, Sep 17, 2014.

  1. jerryberry

    jerryberry

    Joined:
    Feb 12, 2014
    Posts:
    6
    I want to make something like Pong.
    I used transform.Translate but it ignores my rigidbody2D interactions.
    Now I use this script:

    Code (CSharp):
    1. public float speed = 20.0f;
    2. float movementSpeed = 0f;
    3.  
    4. // Use this for initialization
    5. void Start () {
    6.  
    7. }
    8.  
    9. // Update is called once per frame
    10. void Update () {
    11.  
    12.     movementSpeed = Input.acceleration.x * speed;
    13.     rigidbody2D.velocity = new Vector2(movementSpeed, 0f);
    14.  
    15. }
    But it moves the object even if I hold my phone upright (there's e.g. -0.0001 on x axis). My game's orientation is portrait. I want something like this:

    If Input from accelerometer is about 0 - object is in the center of the screen. If it is 0.5 - object is placed on 3/4 of the width of screen. Of course it should be smooth. Now if Input.acceleration is for example 0.3 it goes to the right with appropriate speed and stops at the end of the screen. (there are bounds made with BoxCollider2D).

    I hope you know what I mean :) Sorry for my english.