Input.acceleration

I have this code

using UnityEngine;
using System.Collections;

public class mechanic : MonoBehaviour {
    public float speed = 10.0F;
    // Use this for initialization
    void Start () {
   
    }
   
    void Update (){
            Vector3 pos = transform.position;
            pos.z = -0.13F;
            transform.position = pos;
       
        Vector2 dir = Vector2.zero;
        dir.x = -Input.acceleration.x;

        if (dir.sqrMagnitude > 1)
            dir.Normalize();
       
        dir *= Time.deltaTime;
        transform.Translate(dir * speed);
    }
}

The screens is Portrait, and i want my ball to move left and right though x-axis

But with this code, my ball is jumping, going though stuff, moving up against the gravity

What to do?

I’m guessing you have a Rigidbody on your game object. What you can do then, is modify the Rigidbody by adding force or torque.

Ohh yeah! Thanks, im gonna try that.

Let me guess. when you use translate, it for kinematic object, or object without a rigidbody

I have this code now

using UnityEngine;
using System.Collections;
public class mechanic : MonoBehaviour {
    public float speed = 10.0F;
    void  Start ()
    {
    }
    void Update() {
        Vector2 dir = Vector2.zero;
        dir.x = Input.acceleration.x;
        if (dir.sqrMagnitude > 1)
            dir.Normalize();
    
        dir *= Time.deltaTime;
        Rigidbody2D.AddForce(Vector2 (dir * speed, 0));
    }
}

and im getting this error

Assets/mechanic.cs(21,29): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody2D.AddForce(UnityEngine.Vector2, UnityEngine.ForceMode2D)'

RigidBody is a class, you need an instance of the class to access the AddForce method.

Assuming you have a rigid body component attached to the same game object as this script, do:

using UnityEngine;
using System.Collections;
public class mechanic : MonoBehaviour {
protected Rigidbody2D mBody;   
public float speed = 10.0F;
    void  Start ()
    {
mBody = GetComponent<Rigidbody2D>();
    }
    void Update() {
        Vector2 dir = Vector2.zero;
        dir.x = Input.acceleration.x;
        if (dir.sqrMagnitude > 1)
            dir.Normalize();
  
        dir *= Time.deltaTime;
        mBody.AddForce(Vector2 (dir * speed, 0));
    }
}

I copied your code and im getting these errors now

Assets/scripts/mechanic.cs(17,32): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected



Assets/scripts/mechanic.cs(17,23): error CS1502: The best overloaded method match for `UnityEngine.Rigidbody2D.AddForce(UnityEngine.Vector2)' has some invalid arguments



Assets/scripts/mechanic.cs(17,23): error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.Vector2'