Accelerometer Mapping challenge

Hi
I have been developing an iPhone tilt control, with dead zone, max acceleration by limited tilt and calibrating the iPhone.
The one thing I need to make it perfect is the mapping of the camera following the character smoothly.

I cant get my mind to figure out how to map the following decent:
When I tilt the iPhone left, The character starts running left, and the camera smoothly after a the character so in the end its behind the character again.
This works for forward, left and right direction.
Now comes the tricky part. How should I program going backwards?

My dream scenario is that the character starts going backwards until the camera turns 180 degrees and then the character walks forward… But now the mapping of the iPhone is wrong, because the user is tilting the iPhone backwards and now the character walks forwards. This is an issue because of the following camera.

Has anyone dealt with this kind of issue or got an alternative solution to me problem?

Any idea would be great! I’m totally stucked here.

Here is my code… without any camera rotation.

//////////////////////////////////////////////////////////////
// Tilt2MoveCharacterAndCamera.js
//
//////////////////////////////////////////////////////////////

// This script must be attached to a GameObject that has a CharacterController
@script RequireComponent( CharacterController )

var cameraTransform : Transform;					

// tilt settings
var speed : float = 18;								// Ground speed
var sensitivity : float = 0.1;
var maxSpeedThreshold : float = 0.3;

// jump
var jumpButton : GUITexture;
var jumpSpeed : float = 8;
var inAirMultiplier : float = 0.25; 				// Limiter for ground speed while jumping


private var thisTransform : Transform;
private var character : CharacterController;
private var animationController : AnimationController;
private var velocity : Vector3;						// Used for continuing momentum while in air

function Start()
{
	// Cache component lookup at startup instead of doing this every frame	
	thisTransform = GetComponent( Transform );
	character = GetComponent( CharacterController );	
	animationController = GetComponent( AnimationController );
	
	// Set the maximum speed, so that the animation speed adjustment works
	animationController.maxForwardSpeed = speed;
	
	// Move the character to the correct start position in the level, if one exists
	var spawn = GameObject.Find( "PlayerSpawn" );
	if ( spawn )
		thisTransform.position = spawn.transform.position;	
}

function FaceMovementDirection()
{	
	var horizontalVelocity : Vector3 = character.velocity;
	horizontalVelocity.y = 0; // Ignore vertical movement
	
	// If moving significantly in a new direction, point that character in that direction
	if ( horizontalVelocity.magnitude > 0.1 )
		thisTransform.forward = horizontalVelocity.normalized;
}


function Update()
{
	var acceleration : Vector3 = iPhoneInput.acceleration;
	var calibratedTilt : Vector3 = FixAcceleration (acceleration);
	
	var movement = cameraTransform.TransformDirection( Vector3( 0, 0, 0 ) );
	
	
	if ( -calibratedTilt.y < -sensitivity ) {
		movement = cameraTransform.TransformDirection( Vector3( -calibratedTilt.y, 0, calibratedTilt.x ) );
	}
	else if ( -calibratedTilt.y > sensitivity) {
		movement = cameraTransform.TransformDirection( Vector3( -calibratedTilt.y, 0, calibratedTilt.x ) );
	}
	else if ( calibratedTilt.x < -sensitivity) {
		movement = cameraTransform.TransformDirection( Vector3( -calibratedTilt.y, 0, calibratedTilt.x ) );
	}
	else if ( calibratedTilt.x > sensitivity) {
		movement = cameraTransform.TransformDirection( Vector3( -calibratedTilt.y, 0, calibratedTilt.x ) );
	} 
	else {
		// nothing
	}
	

	//Limit the input coordinates to create max speed a only a litte tilt
	if (movement.x > maxSpeedThreshold ) {
		movement.x = maxSpeedThreshold;
	} else if (movement.x < -maxSpeedThreshold ) {
		movement.x = -maxSpeedThreshold;
	}
	
	if (movement.z > maxSpeedThreshold ) {
		movement.z = maxSpeedThreshold;
	} else if (movement.z < -maxSpeedThreshold ) {
		movement.z = -maxSpeedThreshold;
	}

	// constant speed
	movement *= speed;


	var tabCount : int = iPhoneInput.touchCount;	
	if( tabCount > 0 )
	{
		for ( i = 0; i < tabCount; i++ )
		{
			// Jump, if any of the touches is at the jump buttom.. 
			if ( character.isGrounded  jumpButton.HitTest( iPhoneInput.GetTouch(i).position ) )
			{
				// Apply the current movement to launch velocity
				velocity = character.velocity;
				velocity.y = jumpSpeed * 2;
			} 
		}
			
	}
	
	if ( !character.isGrounded )
	{			
		// Apply gravity to our velocity to diminish it over time
		velocity.y += Physics.gravity.y * Time.deltaTime;
		
		// Adjust additional movement while in-air
		movement.x *= inAirMultiplier;
		movement.z *= inAirMultiplier;
	}

	
	movement += velocity;
	movement += Physics.gravity;
	movement *= Time.deltaTime;
	
	//print(movement);
	// Actually move the character
	character.Move( movement );
	
	if ( character.isGrounded ) {
		// Remove any persistent velocity after landing
		velocity = Vector3.zero;
	}
	
	// Face the character to match with where she is moving
	FaceMovementDirection();	
	
}

private var calibrationMatrix : Matrix4x4;

   //This is the advanced accellerometer code to calibrate the initial iPhoneIput.acceleration input
function CalibrateAccelerometer () {
	var accelerationSnapshot : Vector3 = iPhoneInput.acceleration;
	var rotateQuaternion : Quaternion = Quaternion.FromToRotation(new Vector3(0.0, 0.0, -1.0), accelerationSnapshot);
   
	//create identity matrix ... rotate our matrix to match up with down vec
	var matrix : Matrix4x4 = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1.0, 1.0, 1.0));
   
	//get the inverse of the matrix
	calibrationMatrix = matrix.inverse;
   	
   	//print(calibrationMatrix);
}
   
   	// Make a button to set Calibration function
function OnGUI () {
	GUI.Box (Rect (100,100,270,50), "Tilt to desired view angle");
	if (GUI.Button (Rect (100,250,250,50), "Calibrate Accelerometer")) {
 		CalibrateAccelerometer();
 		
	}
}

//   Get the 'calibrated' value from the iPhone Input
function FixAcceleration (accelerator : Vector3) {
   var fixedAcceleration : Vector3 = calibrationMatrix.MultiplyVector(accelerator);
   return fixedAcceleration;
}

Your accelerometer controller is working in world space, when you want it to work relative to the camera. You will need to transform your accelerometer information with your cameras transform before applying any data to the character.

Hi thanks for the tip, could be give a small code example on what you mean. Im not quite sure I get that suggestion.