Accelerometer to rotate an object to a specific angle within a range

I’m working on a game for Android where you tilt the device side to side to control gravity and affect the path/route of falling objects with physics.

I have set up my game ‘board’ with a fixed camera so rotating the whole object appears to ‘shift gravity’ from the players perspective.

I want the board to tilt no more than about 30 degrees either way around the X axis however at the moment my code isn’t doing what I want. I’m using transform.up to change the rotation of the board according to the accelerometer.

void FixedUpdate () {
		Vector3 dir = Vector3.zero;
		dir = Input.acceleration.normalized;
		dir.y = 0;
		dir.z = 0;
		if (dir.x >= 0.3) {
			dir.x = 0.3f;
		} else if (dir.x <= -0.3) {
			dir.x = -0.3f;
		}
		Debug.Log (dir);
		transform.up = dir.normalized;
	}

Reading the Log output in gameplay shows the Vector3 variable ‘dir’ exactly as I expect it to. If I tilt my device to one side the value gradually increases to 0.3 (or -0.3) and then stays at that value even if I rotate well past it.

The problem is though, currently the board I am rotating just snaps from 90 degrees on it’s left side to 90 degrees on it’s right side, with no range of movement anywhere between the two. It just jumps from one orientation to the other depending on which side of upright the device is tilted.

I’m pretty new to scripting so I may well be missing something obvious. I have had it working how I want it to previously (although without the range limiting) but I can’t see any singificant changes in the code as to why it no longer rotates evenly along with the device.

Any help/suggestions would be much appreciated!

Solved the issue, the problem was caused by setting both the Y and Z axis to 0, as that then means the vector can only point in one of two directions, which then manifested itself as my object ‘snapping’ between those two directions. I solved the issue by setting the Y axis to 0.3f and this has produced the correct behaviour, although I still don’t entirely understand why that doesn’t mean my object isn’t slightly angled along that Y axis.

I know this is a slightly old post but I see a clear mistake in the code because of which you ain’t getting the desired rotation. Since the values are switching between the two sides of zero (-0.3 to 0.3), there need to be two more conditions for when the value should not directly snap to the extreme values (i.e when the accelerometer output is between the max and max range).

void FixedUpdate () {
Vector3 dir = Vector3.zero;
dir = Input.acceleration.normalized;
dir.y = 0;
dir.z = 0;

     if (dir.x < 0.3 && dir.x>0) {
         FinalOutputX = dir.x; } 

     else if (dir.x > -0.3 && dir.x < 0) {
         FinalOutputX = dir.x; }

     else if( dir.x > 0.3 ) {
         FinalOutputX = 0.3;}

     else if( dir.x < -0.3 ) {
         FinalOutputX = -0.3;}
         Debug.Log (dir);

}

The first two statements could be made into one but I prefer having them as two statements so that some things can be tweaked further. However, I haven’t tested this code, but it should work alright. If you understand the logic, you’ll be able to edit it as per your game. And this will solve the snapping issue you were having.
@Alexsutton