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.