# Linear Acceleration in unity 3d?

In above application how can i provide same linear acceleration in unity 3d?
i followed Unity - Scripting API: Input.acceleration
this link but ball can’t move… there is no change in object(sphere/ball) position. In this script i am getting dir=(0,0,0) so that i think ball can’t moving… please tell me the way how to do that.

Thanks to all my viewers.

Well, linear acceleration is easy enough if you understand the physics behind it. Let’s think first in scalar values and then this will be easily translated into vectors. First of all, acceleration is measured in distance over square time (m/s^2 in the metric system), and that means, a “meter per second” per second, so each second, you increase your speed by that amount. So, to implement a linear acceleration to an object in Unity, you can work easily with some basic equations and 3d Vectors to get the very same effect.

Your object has an speed vector, this vector has the direction and magnitude of its speed, the initial value for that vector should be, thus, your initial speed. Then, you might want to apply an acceleration to that vector, it’s as easy as adding each frame a fixed value to that vector, in the direction you wish to have the acceleration. A common case for that is gravity, if you apply a constant acceleration downwards, you’ll have a realistic gravity motion.

That’s the problem, of course, of a frame not being a fixed amount of time, and that is solved with the class member Time.deltaTime, you can check out why does it work on the documentation, but basically multiplying your acceleration (and also speed) Vector by Time.deltaTime, you’re translating the amounts in meters/frame to meters/second, which is a fixed amount of time. If you don’t do that you’ll have your character moving at different speeds on different computers, and even on the same computer the speed may vary on the CPU consume, which you definetly don’t want.

Also, to a more specific implementation, if you’re new to Unity, each object’s position is stored in a Vector3 at its transform.position member (you can call this from whithin any monobehaviour script), modifying this vector over time will result in motion.

Hope it helped!

If you want to create acceleration use:

`````` rigidbody.AddForce(direction*speed,ForceMode.Acceleration);
``````

Using the default one will not do as forces will get added each frame resulting in warp speed within a few second. A simple script like this should get you started:

``````public class Script : MonoBehaviour {
Rigidbody rig;
float move;
float speed = 70;
void Start () {
rig = GetComponent<Rigidbody>();
gameObject.renderer.material.color = Color.blue;
}
void Update(){
move = Input.GetAxis("Vertical");
}
void FixedUpdate () {
print (rig.velocity.magnitude);
if(move != 0)