Reset Accelerometer? Is it possible?

Is it possible to access and modify the accelerometer settings from Unity? I just want the user to be able to set up their configuration (based on how they hold the iphone) before the game starts, but don’t know if it’s possible to actually reset the accelerometer from unity.

Just a quick follow up.

A good example of what I want to accomplish is in the ‘Settings’ of the iPhone game ‘Labyrinth’.

There is no “Setting” for doing this. What all games do that have this option is sample the accelerometer when the player asks to calibrate. The game then applies that sample as an offset to all input which then becomes the input that it uses for gameplay.

The accelerometer will always point in the direction of acceleration.

Ok, that makes sense. But will the accelerometer read past it’s limits (-1 to 1).

For instance if I calibrate in game at say a 45 degree angle (on x axis) so that that’s my center. Once I tilt it to 90 degrees the iphone accelerometer would recognize that as 1, whereas in game would recognize that as being 0.5. If I continue tilting, will it give a value greater than 1?

You simply need to make your own internal representation of the behavior you want and map the accelerometer onto it.

ok cool…thanks a bunch to both of you.

I’m also interested in this but not quite sure where to set something like this.

Using Star Trooper (which is awesome btw) as an example, the iPhone needs to be flat to read zero. To change this to 45 degrees, as sbuchbinder mentioned, is this something you would change in PlayerControls.js?

  • Or does this require a new script entirely?

Here is the default PlayerControls.js script for reference:

var turnSpeed : float = 10.0;
var maxTurnLean : float = 50.0;
var maxTilt : float = 50.0;

var sensitivity : float = 10.0;

var forwardForce : float = 1.0;
var guiSpeedElement : Transform;

private var normalizedSpeed : float = 0.2;
private var euler : Vector3 = Vector3.zero;

var horizontalOrientation : boolean = true;

function Awake () {
	if (horizontalOrientation)
		Screen.SetResolution(480, 320, true);
	else
		Screen.SetResolution(320, 480, true);

	guiSpeedElement.position = new Vector3 (0, normalizedSpeed, 0);
}

function Update () {
	for (var evt : iPhoneTouch in iPhoneInput.touches)
	{
		if (evt.phase == iPhoneTouchPhase.Moved)
		{
			normalizedSpeed = evt.position.y / Screen.height;
			guiSpeedElement.position = new Vector3 (0, normalizedSpeed, 0);
		}
	}
}


function FixedUpdate () {
	rigidbody.AddRelativeForce(0, 0, normalizedSpeed * forwardForce);
	
	var accelerator : Vector3 = iPhoneInput.acceleration;

	if (horizontalOrientation)
	{
		var t : float = accelerator.x;
		accelerator.x = -accelerator.y;
		accelerator.y = t;
	}
	
	// Rotate turn based on acceleration		
	euler.y += accelerator.x * turnSpeed;
	// Since we set absolute lean position, do some extra smoothing on it
	euler.z = Mathf.Lerp(euler.z, -accelerator.x * maxTurnLean, 0.2);

	// Since we set absolute lean position, do some extra smoothing on it
	euler.x = Mathf.Lerp(euler.x, accelerator.y * maxTilt, 0.2);
	
	// Apply rotation and apply some smoothing
	var rot : Quaternion = Quaternion.Euler(euler);
	transform.rotation = Quaternion.Lerp (transform.rotation, rot, sensitivity);
}

I haven’t gotten around to it yet, but I was thinking if you could convert the gravity vector to Euler angles, then instead of trying to set everything to ‘0’, you would just store a reference value that you will consider ‘0’ and measure the deviation from that point and the degree of deviation is your input value.

So for example, if the user is holding the device at 45 degrees about the X-axis, and you want that to be ‘0’, just store 45 degrees as the “neutral” angle, then if it changes to 90 or 0 degrees, you’d subtract your “neutral” angle from that and get how far the device has moved from “neutral”.

If you get this implemented, I’d really appreciate it if you could share your findings in this thread. If I get there first (probably be a few days before I can get to it), I’ll post my findings as well.

Sounds good. I’ll see if I can get my head around that. :smile:

After looking at this some more I found an easy way to set “flat” closer to 45 degrees.

Obviously, user set calibration is the end goal, but in the meantime this allows up and down ship motion from an easier-to-view angle.

Replace the ‘if (horizontalOrientation)’ section of the ‘function FixedUpdate’ in PlayerControls.js with the following:

if (horizontalOrientation)
	{
		var t : float = accelerator.x;
		accelerator.x = -accelerator.y;
		accelerator.y = t + 0.7;
	}

Adjust the 0.7 to match your desired angle (0.7 feels like 45 degrees to me, give or take).

This approach does not give you the full rotation to motion.

Hello All,
so well i’m also interested in this but can’t get it to work.
Well i want to learn from Startrooper but i’m not able to change the orientation…
Well is there a Script for the calibration of the iPhone orientation ?
Would be great if someone cold help out a little…
So what i want when Startrooper starts is a Button for calibrating the iPhone and if i press ok the Game starts.
Many thxxx
Stefan

Hi,

here is how you get the full up and down rotation:

accelerator.y = (accelerator.y + 0.5) * 2;

This will set the “flat” zero orientation to exactly 45 degree.

If you want to reverse it just use a table and use “rule of three” to map the values to the acceleration output. 0 deg = 1; 45 deg = 0; 90 deg = -1

Hope this will help you to find the way also set other orientations.

As one note, if you set this orientation, you have to recalculate the turn also a little :wink:

Thxx Akano !!!
That’s a usefull answer !!!
I miss it a lot here in this forums…
Many keep the secrets for themselfs… :wink:
Thank you awesome help !
Stefan

I’m sorry, but that’s plain wrong; this is the most helpful forum I’ve seen. If you don’t get an answer, 99% of the time it’s because A) it’s plainly in the docs, or B) it’s been covered many times on the forums already, or C) it’s a basic programming question that doesn’t really have anything to do with Unity per se, or D) nobody knows.

–Eric

1 Like

@MadMac - Eric said it best.

@Akano - Nice job on the rotation fix! :smile:

I use…

function FixedUpdate () {
	var dir : Vector3 = Vector3(0, 0, 0);
		
	dir.x = iPhoneInput.acceleration.y;
	dir.y = (iPhoneInput.acceleration.x)-tilt;
	dir.z = shipSpeed;

I tested this, it does not work, it simply does a nose dive into the ground.

if (horizontalOrientation) 
   { 
      var t : float = accelerator.x; 
      accelerator.x = -accelerator.y; 
      accelerator.y = (accelerator.y + 0.5) * 2;
      //accelerator.y = t; 
   }

Is this how you intended the code to work?
I wish Unity would supply an example just on accelerator manipulation since a bunch of people have different theories on how it should work for this example.

Me too interested here. So, Akano’s answer is the final one :?:
I’ve not given try to any yet. All this need to be done in the Game Settings screen, so that the user can adjust the settings as preferable :?:

I posted my previous question after watching the responses on the first page only. I just couldn’t see the next page then.