why is the cube moving when I use a controller, but not with the accelerometer?

void Update () {
    text.text = "X: " + Input.acceleration.x + "; Z: " + Input.acceleration.z;
    //transform.position = new Vector3(10* Input.acceleration.y, 0, 10* Input.acceleration.z);

    transform.Translate(Input.acceleration.x, 0, Input.acceleration.z);

    //transform.position = new Vector3(10* Input.GetAxis("Horizontal"), 0, 10* Input.GetAxis("Vertical"));

    transform.Translate(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

(I'm on a Xperia Z3 compact by the way)


I guess you might need to apply some speed so that it works on the phone. Might be the Input.acceleration.x value is not high enough on phone, but works on the controller. Also it is advisable to use Time.deltaTime to keep the speed constant on all device, else it will change as per the device processing power.

 public float speed = 10.0f;
 void Update () 
        transform.Translate(Input.acceleration.x*speed*Time.deltaTime, 0, Input.acceleration.z*speed*Time.deltaTime);

| improve this answer | |
  • \$\begingroup\$ Suddenly it works for some reason O.o (without modification) - what about "transform.position = 'vector3 from accelerometer'" though? \$\endgroup\$ – 000000000000000000000 Aug 15 '15 at 21:05
  • \$\begingroup\$ That will not move it beyond a particular distance as the values from accelerometer will reach a threshold. Try using += on individual axis so that it moves. \$\endgroup\$ – Hash Buoy Aug 15 '15 at 21:09
  • \$\begingroup\$ I wanted it to not move further than a particular threshhold, so the position mirrored the orientation of the device. Like when Input.accelerometer.x is at 0.7 the transform.position.x should be 0.7 * 10 \$\endgroup\$ – 000000000000000000000 Aug 15 '15 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.