gradually moving an object up to speed rather then instantly?

i want to know how to, on button press, make an object move without instantly getting to the speed i set it to move at. right now im using this script

function Update () {
if (Input.GetKey ("left"))
    transform.position.x = transform.position.x + -0.10;
if (Input.GetKey ("right"))
    transform.position.x = transform.position.x + 0.10;

}

which works, but when the button is pressed the object instantly moves at 0.10, i would like it to accelerate to 0.10 then keep moving at that, then when the button is released de-accelerate back to not moving. i hope this makes sense :/

This code will work for you:

var Speed        : float = 0;//Don't touch this
var MaxSpeed     : float = 10;//This is the maximum speed that the object will achieve
var Acceleration : float = 10;//How fast will object reach a maximum speed
var Deceleration : float = 10;//How fast will object reach a speed of 0

function Update () {
if ((Input.GetKey ("left"))&&(Speed < MaxSpeed))
    Speed = Speed - Acceleration * Time.deltaTime;  
else if ((Input.GetKey ("right"))&&(Speed > -MaxSpeed))
    Speed = Speed + Acceleration * Time.deltaTime;
else
{
    if(Speed > Deceleration * Time.deltaTime)
        Speed = Speed - Deceleration * Time.deltaTime;
    else if(Speed < -Deceleration * Time.deltaTime)
        Speed = Speed + Deceleration * Time.deltaTime;
    else
        Speed = 0;
}

transform.position.x = transform.position.x + Speed * Time.deltaTime;
}

Try lerping it, here is the documentation:

transform.position = Vector3.Lerp(start.position, end.position, Time.time);

This gradually moves between two points. The movement is constant over time, to have the movement gradual (like building momentum) at the start and stop points I believe you must call lerp with the current position like so:

transform.position = Vector3.Lerp(transform.position, end.position, Time.time);

Yes, actually just look at the second example on that link. :-). Duh.

For 2D platformer movement with some acceleration and deceleration…

 void Update()
    {
        float h = Input.GetAxis("Horizontal");

        Rigidbody2D rbody = GetComponent<Rigidbody2D>();

          rbody.velocity = Vector3.right * h * speed; 
    }

public float speed = 0f;
public float maxSpeed = 10f;
public float forceMagnitude = 2f;

void FixedUpdate() {
	    // Store the input axes.
	    float h = Input.GetAxisRaw("Horizontal");
	    float v = Input.GetAxisRaw("Vertical");

	    // Move the player around the scene.
	    Move (h, v);

	    //other stuff...
	}

void Move(float h, float v) {
		if (v != 0 || h != 0) {
			// Set the movement vector based on the axis input.
			movement.Set (h, 0f, v);
			speed = Mathf.Min(speed + forceMagnitude * Time.deltaTime, maxSpeed);
		} else {
			speed = Mathf.Max(speed - forceMagnitude * Time.deltaTime * 1.5f, 0);
		}
		//Debug.Log (speed);

		// Normalise the movement vector and make it proportional to the speed per second.
		movement = movement.normalized * speed * Time.deltaTime;
	    // Move the player to it's current position plus the movement.
	    playerRigidbody.MovePosition(transform.position + movement);
	}

@IJM

Dude, you are a legend sir. I spend Days trying to get this to work with a script 3 pages long and still pulling out hair. Your solution was in Java i converted into c# and it worked marvelously. Thank you.
here is what worked for me in C#

using UnityEngine;
using System.Collections;

public class test2 : MonoBehaviour
{
public Vector3 position;
float Speed = 0; //Don’t touch this
float MaxSpeed = 10; //This is the maximum speed that the object will achieve
float Acceleration = 10; //How fast will object reach a maximum speed
float Deceleration = 10; //How fast will object reach a speed of 0
// Use this for initialization
void Start()
{
transform.position = position;
}

// Update is called once per frame
void Update()
{

    if ((Input.GetKey("left")) && (Speed < MaxSpeed)) Speed = Speed - Acceleration * Time.deltaTime;
    else if ((Input.GetKey("right")) && (Speed > -MaxSpeed)) Speed = Speed + Acceleration * Time.deltaTime;
    else
    {
        if (Speed > Deceleration * Time.deltaTime) Speed = Speed - Deceleration * Time.deltaTime;
        else if (Speed < -Deceleration * Time.deltaTime) Speed = Speed + Deceleration * Time.deltaTime;
        else
            Speed = 0;
    }
    position.x = transform.position.x + Speed * Time.deltaTime;
    transform.position = position;

}

}

float Speed = 0;
float MaxSpeed = 3f;
float Acceleration = 2f;
float Deceleration = 5f;

if (Input.GetKey (KeyCode.Mouse0)) {
	Speed = Mathf.Min (Speed + Acceleration * Time.deltaTime, MaxSpeed);
} else {
	Speed = Mathf.Max (Speed - Deceleration * Time.deltaTime, 0f);
}

Is it possible to make the object from slow to fast automatically as time passes without pressing any key. Im making a shooting game and the object will move horizontally…Im very new to this unity stuffs. Can anyone help?