- Home /
How to calibrate accelerometer according to start ?
I think i have a "should be" working code but i don't know why it is not working. With this code too i have to hold the phone straight to get it to work. My present code is below. Any help is appreciated :)
#pragma strict
var speed : float;
var accelerationX : float = 0;
var accelerationZ : float = 0;
var zatstart : float = 0;
function Start () {
zatstart = Input.acceleration.z;
}
function Update () {
var dir : float;
dir = (Input.acceleration.z-zatstart);
if (dir > 1){
dir = 1;
}
accelerationX = Input.acceleration.x;
accelerationZ = -1*(dir);
GetComponent.<Rigidbody>().AddRelativeForce(new Vector3(accelerationX * speed,0,accelerationZ * speed));
}
Have you tried using AddForce ins$$anonymous$$d of AddRelativeForce ?
Answer by tusadi · Dec 18, 2016 at 12:11 PM
I had modified the Roll-a-ball tutorial by using my smartphone's accelerometer.This is what I had done
public float speed = 6;
private RigidBody rb;
private float zatstart = 0;
private float xatstart = 0;
private float hzMovement = 0, vtMovement = 0;
public Button calibrate;
public Toggle accel;
void Start(){
calib();
rb = GetComponent<Rigidbody> ();
}
void Update(){
calibrate.onClick.AddListener (calib);
if (accel.isOn) {
hzMovement = Input.acceleration.x - xatstart;
vtMovement = -Input.acceleration.z + zatstart;
} else {
hzMovement = CrossPlatformInputManager.GetAxis ("Horizontal");
vtMovement = CrossPlatformInputManager.GetAxis ("Vertical");
}
Vector3 v = new Vector3 (hzMovement, 0, vtMovement);
rb.AddForce (v * speed);
}
void calib(){
xatstart = Input.acceleration.x;
zatstart = Input.acceleration.z;
}
The code will be pretty much same in both javascript and c#.Hope this helps.
Your answer
Follow this Question
Related Questions
Help with android shake intensity. 0 Answers
How to make Headbobber on joystick Android 2 Answers
screenCanDarken for Android (Backlight going dark) 1 Answer
Android Plugin Help 1 Answer
Accelerator and how to limit player movement (Android) 0 Answers