- Home /
accelerometer speed
Hey guys I was looking at the accelerometer tutorial that Unity made.
I've been trying to understand how to limit the speed. Right now it shoots off at a thousand miles per hour if I tilt it more than a centimeter.
There's no much to look at, but I'm only wanting to use the accelerometer for the X axis.
Here is the basic code that I have.
public class Controls : MonoBehaviour {
// Variables dealing with Speed and boost.
public float speed;
public int sideSpeed = 1.0;
void Start(){
}
// Update is called once per frame
void Update () {
//Use this for constant forward movement
transform.Translate (Vector3.forward * speed * Time.deltaTime);
//Use this for the accelerometer for X Axis.
transform.Translate (Input.acceleration.x, 0, 0 * sideSpeed * Time.deltaTime);
}
}
I've tried doing it with Addforce, but even if I change the force to 50000 it just moves in slowmotion.
I've seen stuff about clamps, but I don't understand the clamping for this. I'm new to mobile development.
Please help me.
Thank you!
Answer by NickP_2 · Nov 01, 2014 at 06:09 PM
Use a rigidbody with addforce in a FixedUpdate() function instead of the Upate function.
Mathf.Clamp(float value, float min, float max) just returns a value between 2 given, if the value is lower than the min, the value will be min, if it's higher than max, it'll be max. for example:
float value = Mathf.Clamp(10, 2, 3);// value will be 3, because 10 is bigger than 3.
float value = Mathf.Clamp(10, 20, 30);// value will be 20, because 10 is smaller than 20.
as for the rigidbody, it should look something like this:
void FixedUpate()
{
Vector3 force = new Vector3(Input.acceleration.x, 0, 0) * sideSpeed;
rigidbody.AddForce(force);
}
I did do it in a Fixed Update, but I didn't do it the way you did!
THAN$$anonymous$$S IT WOR$$anonymous$$S!!!!!!!!!!!!
I'm used to PC stuff so this is a bit different for me. But I did it and it works GREAT!
You are a lifesaver!