Android tilt controls - taking player direction into consideration

Hi,

I’m having an issue with my game with the tilt controls. I’ve got the tilt controls working initially but when the player turns 90 or -90 degrees (I have made a game similar to temple run with the movement, the player is always running and a screen swipe will rotate the player + or - 90 degrees), the tilt controls continue to control the same way, so the player will just run faster or slower rather than left or right. I understand why this happens - the code just takes into consideration one axis’ tilt and controls the player in the same way. I’m just really unsure how to change this for what I need.

How can I change this so that the tilt controls will affect the way the player is facing rather than generally? I feel this may be something more obvious but I’ve been thinking a lot and have done lots of research to no avail.

Here is my code segment for the tilt movement at the moment (javascript), just very basic:

direction.x = Input.acceleration.x * moveSpeed;

if (direction.sqrMagnitude > 1)
direction.Normalize();
 
// Make it move 10 meters per second
direction *= Time.deltaTime;
 
// Move object without transform to take boundaries into consideration
controller.Move (dir * speed); 

Any help with this would be greatly appreciated. Thanks.

I beleive you need to change last line to this:

controller.Move (direction * speed);

add one line of code to rotate your controller:

direction = transform.TransformDirection(direction);

In Unity 4.1 is not working. Any solution? When I compile in 3.5 has no problems, but I made a stupid opening my project in Unity 4.1 and I have to finish the game on it. Someone has the same difficulty?