Accelerometer

I wanted my object(ring) to move from left to right. but the code we have here seems to work but the movement is too fast. what else can we do to make it move from left to right with not too fast not too slow movement.

using UnityEngine;
using System.Collections;

public class Accelo : MonoBehaviour {

public Transform carTransform;
private float[] pos =   new float[] {-70.0f, 0.0f, 70.0f};
private int speed = 2;
// Use this for initialization
void Start () {

}

void Update() {
	Vector2 dir = Vector2.zero;
	dir.x = Input.acceleration.x;
	int z = 5;
	int y = 10;

	if(y>z){
		rigidbody.AddForce(Vector3.forward*2);
	}

	if(dir.x < -0.21)
	{
		float x = Mathf.Lerp(carTransform.position.x,pos[0],Time.deltaTime);
		carTransform.position = new Vector3(x, 0, 0);
	}
	
	if(dir.x > 0.21)
	{
		//TurnWheelsRight();
		float x  = Mathf.Lerp(carTransform.position.x,pos[2],Time.deltaTime);
		carTransform.position = new Vector3(x, 0, 0);
	}

}

}

Thank you :smiley:

If you want to keep using Lerp, change the third argument in lines 21 and 28 of your pasted code to 1 - Mathf.Exp(-RateControl * Time.deltaTime) and fiddle with the value of RateControl until you’re happy with it.

Or you could fix a maximum movement speed instead and use Vector3.MoveTowards. It takes the same three arguments but in this case the last argument should be MoveSpeed * Time.deltaTime.

Finally you could give the car some inertia, meaning it would need time to speed up and slow down. This is a little more complex because you need to track its velocity, but one you’re storing velocity you can apply the damped spring equation to get a good result.