- Home /
Sensitivity on Android Input.acceleration
I'm making a simple ball game where the ball rolls when the device is tilted. I want it to be less sensitive, so that when the user is holding is somewhat parralel to the ground, it pretends it's perfect and it does not add any force.
My code so far: (a modification of Unity's sample script)
// Move object using accelerometer
var speed = 100.0;
var sensitivity = 0.2;
function FixedUpdate () {
var dir : Vector3 = Vector3.zero;
// we assume that device is held parallel to the ground
// and Home button is in the right hand
// remap device acceleration axis to game coordinates:
// 1) XY plane of the device is mapped onto XZ plane
// 2) rotated 90 degrees around Y axis
dir.x = Input.acceleration.x * 5;
dir.z = Input.acceleration.y * 5;
Mathf.Clamp
// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();
// Make it move 10 meters per second instead of 10 meters per frame...
dir *= Time.deltaTime;
// Move object
rigidbody.AddForce(dir * Physics.gravity.magnitude);
}
The code I've tried (and it failed):
if(dir.x < 0.1 && dir.x > -0.1) { dir.x = 0;}
if(dir.z < 1.1 && dir.z > 0.9) { dir.z = 1;}
If you're reading this, thank you for your time.
Answer by meat5000 · Dec 01, 2013 at 11:16 PM
dir.x = Input.acceleration.x * 5;
dir.z = Input.acceleration.y * 5;
What about making '5' a smaller number?
I dont want to slow it down, rather have it snap into the straight position so that it doesent apply any force when the user thinks he's holding it straight. (thanks for the answer, btw)
I'm thinking that the extra factor (the 5) in this case will not help, as dir is Normalised. Remove the 5 to reduce sensitivity and add the factor somewhere else ins$$anonymous$$d. It may help keep things smooth.
Answer by SilverRush · Aug 02, 2014 at 12:33 PM
if(dir.x < 0.1 && dir.x > -0.1) { dir.x = 0;}
if(dir.z < 1.1 && dir.z > 0.9) { dir.z = 1;}
Try them again and Debug.Log (dir) before and after these code to see what happened. Make sure you add these code before you normalise them.
They should work, I think.
Another possibility is that though the Force is removed, velocity is still there and appears to be not working.